Create a table with a single column in Matlab - 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

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

Find first zero row in a 2 column array in MATLAB

I have an array with many rows and two columns. I wish to find the first row for which both entries are zero in the array. I have read this post but am unsure how to adapt the find command for a 2 column array.

Select column from matrix where some other column satisfies given condition

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)

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.