Select column from matrix where some other column satisfies given condition - matlab

My matrix has 10 columns. I want to select all rows where the first column is less than 4. If I use
data(data(:,1)<4)
Only the first column is selected.
How do I display all corresponding column values?
How do I select a single corresponding column, e.g., select column 2 where value of column1<4?

How about
data( data(:,1) < 4, : )

data(data(:,1)<4,:)
: indicates all columns. Since data is 2-d matrix, you need to input two parameters, one for row and one for column.
If you need specific columns like column 2
data(data(:,1)<4,2)

Related

How to get a collective output of multiple loop run using a selection condition in Matlab?

I have a table (L-arrival) of 279 rows and 252 columns. Only the first column has values while others are just NaN. The cells in the first column have multiple values (i.e. some have 1, some have 4 number of values). First of all, I am trying to select a single maximum value from each cell of the first column so that I can have a column of a single value for each cell only. Then I want to do this in a loop so that for every new value that I get, they are sorted and only the maximum values are chosen. Finally, I want to make a collection of these values obtained from multiple runs for each cell. Can anyone suggest to me how it can be approached in MatLab?I tried using the following code but didn't work well.
for b=1:279
m = numel(cell2mat(L_arrival(b,1)));
g(b)=mat2cell([cell2mat(g(b)); cell(L_arrival(b,1))]',[1 2]);
end

Create a table with a single column in Matlab

Why does this not work?
cell2table({1;2;3},'RowNames',{'test'})
I have defined the data to be a cell column vector of numbers, and have specified a single heading.
I get the error:
Error using cell2table (line 58)
The RowNames property must contain one name for each row in the table.
creating a table with 1 row and 3 columns and the row named 'test':
cell2table({1 2 3},'RowNames',{'test'})
creating a table with 3 rows and 1 column and the cloumn named 'test':
cell2table({1;2;3},'VariableNames',{'test'})
you can also combine those, but 'Rownames'length hast to equal the amount of rows and 'Variablenames' length has to equal the amount of columns

MATLAB matrix operation

I am having matrix with approx 3000 rows(changing) and 3 columns.
I have count of both rows and columns.
I am trying to plot the graph:
x=1:3000;
plot(matrix(x,1))
is there any way that I can include all rows in the plot instruction itself so that I can remove 'x=1:3000' ?
Also, I want to divide, 1st column of matrix which have 3000 rows into another matrix of 3 columns each with 1000 rows. Any specific instruction for this ?
I have made for loop for this and then i am placing individually the elements in the new array. But its taking long time.
As to the plotting issue, using the colon operator will plot all rows for your desired column:
plot(matrix(:,1));
EDIT: You mentioned you were a beginner. In case you haven't seen the colon operator used like this before, a colon operator all by itself when indexing into a matrix essentially means "all __", either "all rows" if in the first position or "all columns" if in the second position.
As for the second question, of splitting one column into a new matrix with multiple columns, you can use the reshape() function, which takes the input matrix to be reshaped and a number of output rows and columns. For example, to split the first column of matrix into 3 columns and put them into newMatrix, use the following:
newMatrix = reshape(matrix(:,1),[],3);
Note that the above code uses [] in the second argument (the number of rows argument) to mean "automatically determine number of rows".This is automatically determined based on the number of columns, which is defined in the third argument here as 3. The reshape function requires that the number of output rows * output columns be equal to input rows * input columns. So in the above case this will only work if the starting matrix has a number of rows which is divisible by 3.

Taking average of one column with w.r.to other column

I have two columns in .std file. I want average of the second column values corresponding to all values ranging from some value (eg. 1.0- 1.9) in first column how can I program in Matlab?
Say, a is the name of your two column matrix. If you want to find all of the values in the first column in the range of 1.0 - 1.9 and then use those entries to find the mean in the second column you can do this:
f = find(a(:,1)>=1 & a(:,1)<=1.9)
m = mean(a(f,2))
find will find the values that lie within this range and return the index, and a(f,2) accesses those indices in the in the second column and takes the mean. You can also do it with one line like so:
m = mean(a((a(:,1)>=1 & a(:,1)<=1.9),2))

Multiple Columns, way to select closest to a value

I'm trying to analyze data sets that are obtained from CSV files. After the data is read into matlab, I am left with a variable of my data only. The number of columns and rows changes between each file. Is there a way to average each column and then create a variable for the one with the closest average to a certain value? and then also select the columns directly before and after this middle column and create variables for them, as well as create a variable for the column with the lowest average? Currently, I am selecting the columns manually and creating a variables for them that way.
For example:
I have this table of numbers. (I used the same number in each column for sake of easy averaging in this example.
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Let's say I want the column whose average is closest to 3.2
That column would be column 3 whose average is 3. Then I would want the code to select the column before (column 2) and the column after (column 4). As well as the column with the lowest average (column 1)
First get the averages (I assume the data matrix is in variable X):
Xmns = mean(X);
Then to find the minimum, use "min":
[val,ind] = min(Xmns);
"val" holds the minimum value, "ind" the corresponding index in Xmns, which is the corresponding column.
To find the column mean closest to a particular value, again you can use min:
[val,ind] = min(abs(Xmns-key_val));
Now "ind" holds the column index with mean closest to "key_val". The next column is just "ind+1" and the previous "ind-1" - just be sure to check you are not beyond the ends of the matrix (i.e. ind may already be 1 or size(X,2)).
Also, given the column index "ind", to create a new variable with that column, you just use:
sc= X(:,ind);
and if you want to remove that column from X:
X(:,ind) = [];
and that is all.