Clear Filters
Clear Filters

How to find the total number of elements in individual row of a cell array?

5 views (last 30 days)
C = {[1; 2; 3]; [2; 1; 3, 4]; [3; 1; 2]; [1; 2]};
Here,
in first row, elements are 1,2,3,1 | total number of elements = 4
in second row, elements are 2,1,1,2 | total number of elements = 4
in third row, elements are 3,3,2 | total number of elements = 3
in fourth row, elements are 4 | total number of elements = 1
% I was trying this code
sum = 0;
for i = 1:4
C{i,1}(1);
sum = sum + 1;
end
sum % it gives me the output: total number of elements in row 1 which is 4.
% but error occurs when I write
sum = 0;
for i = 1:4
C{i,1}(3); % row 3
sum = sum + 1;
end
sum % it gives error
% Can you please help me to solve this issue?
  2 Comments
Bob Thompson
Bob Thompson on 19 Nov 2018
What exactly is the error you are getting? And which line is producing it?
If all of your cells contain single column arrays then it might be easier to count based on the length of the arrays.
for i = 1:size(C,1);
lngth(i) = length(C{i}) % Number of elements in each cell
end
for j = 1:max(lngth)
row(j) = sum(lngth>=j); % Number of elements in each 'row'
end

Sign in to comment.

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!