How to find the total number of elements in individual row of a cell array?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Bob Thompson
2018-11-19
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
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!