How to get a collective output of multiple loop run using a selection condition in Matlab? - 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

Related

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.

MATLAB loop over unique function output

following problem:
I have a very large matrix and several rows share the same identifier in column 1. For these rows I need to do some averaging, reformatting etc.
Currently I am identifying all unique identifier values in column 1 by using the function unique and then do averaging, reformatting of values in other columns within this loop for each set of rows sharing the same column 1 value within a loop.
ID = unique(data.1);
for i = 1:length(ID);
do stuff
end
I guess this is highly inefficient and slow but I cannot think of a better way of handling this.

Selecting cells in a table using for-loop

I need to obtain individual values from cells incrementally.
For example in a table called "T":
Can Matlab create a loop to yield the following?:
T(1,1)
T(1,2)
T(1,3)
T(1,4)
and so forth.
First of all the first parameter in MATLAB equals the row, the second the column. So following your picture you would want to extract a complete column. Then you have to loop over your first parameter and keep the 2nd constant.
So not sure if this is really what you want but:
for k = 1:size(T,1)
T(k,1)
end
would write all elements in column 1 row after row. This is because size(T,1) returns the No. of rows in your Table. So by looping over all rows you return one column. Easier with the same result would be:
T(:,1)
which would return the whole first column.
If you don't want them to be displayed but to use them as entries for another variable use:
new_T = T(:,1);
The ; makes sure that the command isn't displayed on the console. And the new_T would have all entries from column 1.
Another example:
new_T2 = T(1:500, 4);
This would result in the first 500 elements from column 4.
Hopefully this solves your question, if not please explain your question more detailed.

calculating the number of columns in a row of a cell array in matlab

i've got a cell array full of numbers, with 44 rows and different column length in each row
how could i calculate the number of columns in each row?(the columns which their contents are not empty)
i've used 2 different ways which both of them where wrong
the 1st one:
%a is the cell array
s=length(a)
it gives 44 which is the number of rows
the 2nd one
[row, columms]=size(a)
but it doesn't work either cause the number of columns is different in each row.
at least i mean the number of columns which are not empty
for example i need the number of columns in row one which it is 43(a{1 1:43}) but it gives the number of columns for each elements like a{1,1} which is 384 or a{1,2},a{1,3} and so on
You need to access each member of the cell array separately, you are looking for the size of the data contained in the cell - the cell is the container. Two methods
for loop:
cell_content_lengths=zeros(1,length(a));
for v=1:length(a)
cell_content_lengths(v)=length(a{v});
end
cellfun:
cell_content_lengths=cellfun(#length,a);
Any empty cells will just have length 0. To extend the for-loop to matrices is trivial, and you can extend the cellfun part to cells containing matrix by using something like this, if you are interested:
cell_content_sizes=cell2mat(cellfun(#length,a,'uniformoutput',false));
(Note for the above, each element of a needs to have the same dimension, otherwise it will give errors about concatenating different size matrices)
EDIT
Based on your comment I think I understand what you are looking for:
non_empty_cols = sum(~cellfun(#isempty,a),2);
With thanks to #MZimmerman6 who understood it before me.
So what you're really asking, is "How many non-empty elements are in each row of my cell array?"
filledCells = ~cellfun(#isempty,a);
columns = sum(filledCells,2);

how to use tables in Matlab

I want to know please how can I fill in a table, row by row, by numbers, and then to color in each row the cell that has the higher number in it.
I searched the web a little and found this "set(handles.uitable2, 'Data', {5,6,4})" but this is not helping me because i need to fill in row by row, and in this method the row data is been replace.
this is the table. as you see there is 7 rows and 10 columns. in each columns there is the correlation score of the plate digit against the samples digits (0-9).
this is how I call the correlation function[scores] = compute_corr(digit); I'm executes this call 7 times for each plate digit. scores is an array that saves in each call the correlation scores and digit is one digit from the plate.
thanks in advance.
I don't believe there's a way to update the data incrementally. So you should maintain an array containing your data, update that row by row, and call set(...,'Data',actualData) when it changes.