calculating the number of columns in a row of a cell array in matlab - 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);

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

extract the content of each cell in MATLAB

I have an array called (dataBig2) that contains 20234 cells.
Each cell contains N number of rows and 9 columns (the first 7 columns are integer and the last 2 are 'string'). please see the attached image
I'd like to obtain the actual content of each cell and store it in an array.
for example: the content of first cell is 30*9 (see the attached image)
I have tried this code so far! but unfortunately, I didn't get the content of each cell!!
by the way i cant use cell2mat because the content of the cell is not the same data type
for i=1:size(dataBig2,2)
final{i}=dataBig2{i}(:,:);
end
To concatenate all of the n x 9 arrays into one large N x 9 array, you can use vertcat:
final = vertcat(dataBig2{:});
dataBig2{:} puts the individual elements (the n x 9 arrays) of the cell array into a comma-separated list, which vertcat concatenates vertically.

How can i change indices of a matrix in Matlab?

My problem is i want to assign some numbers to indices of a matrix. For example if I remove first row and first column of a matrix, then in remaining matrix 3th row and 4 column would actually be 4th row and 5th column in the first place.
I can do it with Array1(Array2) , however my code will have many seperate recursions so it is frustrating to keep track of everything. So, is there an once and for all way to map original 1..n indices to remaining matrix even after I remove rows and columnsth
Thanks in advance
You can do something like this as per beaker's suggestion
originalMatrix = magic(4)
dimension = size(originalMatrix)
indexMatrix = zeros(dimension(1), dimension(2))
for i = 1:numel(indexMatrix)
indexMatrix(i) = i
end
and remove the required row and column from indexMatrix.

How to take the mean of columns from an array within an array?

I am currently working on a project in Matlab where I have a cell array of cell arrays. The first cell array is 464 columns long and 1 row deep. Each of these cells is another cell array that is 96 columns and 365 rows. I need to be able to get the mean of the 96 columns for each of the 464 arrays and place each of the 464 arrays on a different row in a new array called mean. I have tried to write code to just do one column as follow:
mean = Homes{1,1}(1:)
But I when ever I try to run this code I got the follow error:
mean = Homes{1,1}(1:)
|
Error: Unbalanced or unexpected parenthesis or bracket.
Basically my final array name mean needs to be 96 columns by 464 rows. I am stuck and could really use your help.
Thank you.
I suggest you to try the following code on a smaller matrix. See if it gives you the desired results.
a=cell(1,4); %for you it will be a=cell(1,464)
for i=1:4
a{i}=randi(10,[5 10]); %for you it will be a{i}=randi(10,[365 96]);
end
a1=cell2mat(a); %just concatenating
a1=mean(a1); %getting the mean for each column. in your case, you should get the mean for 96*464
a2=reshape(a1,[10 4]); %now what reshape does it it takes first 10 elements and arranges it into first column.
%Therefore, since I finally want a 4x10 matrix (in your case 464x96), i.e. mean of 10 elements in first cell array on first row and so on...
%Thus, 1st 10 elements should go to first column after doing reshape (since you want to keep them together). Therefore, instead of directly making matrix as 4x10, first make it as 10x4 and then take transpose (which is the next step).
a2=a2'; %thus you get a 4x10 matrix.
In your case specifically, the code will be
a=cell(1,464);
for i=1:464
a{i}=randi(10,[365 96]);
end
a1=cell2mat(a);
a1=mean(a1);
a2=reshape(a1,[96 365]);
a2=a2';

Identifying uniques in a cell array

I have a 45x2 cell in MATLAB, with the first column an arbitrarily sized matrix of doubles.
Some of these matrices are repeated, whilst others aren't. I'm attempting to strip out only the unique matrices (but recording the number of repeates), and keep the second column as is.
I've tried a number of things (tabulate, hist et al) but they all fail because of the cell structure (I think). How would one go about doing this, short of looping through each of them individually?
If you convert your matrices to strings, you can run unique on them:
%# create a sample cell array
mc = {magic(3);magic(4);magic(4);magic(5);magic(3);magic(4)}
%# convert to strings
mcs = cellfun(#(x)(mat2str(x)),mc,'uniformoutput',false);
%# run unique
[uniqueCells,idxOfUnique,idxYouWant] = unique(mcs);