How to count the number of elements of several arrays inside an array?

조회 수: 2 (최근 30일)
Jasmine Karim
Jasmine Karim 2018년 8월 28일
댓글: OCDER 2018년 8월 30일
Not sure if there is an easy function for this. I have a 3x3 cell array, and inside each of those cells are arrays of various sizes. I want to calculate the length for each of these and store it, am trying to write a loop for this but not sure how to get it to read all the arrays vertically AND horizontally. Any suggestions?
18x5 cell 25x5 cell 46x5 cell
63x5 cell 77x5 cell 27x5 cell
34x5 cell 90x5 cell 19x5 cell
ie) the output would be {18, 25, 46; 63, 77, 27; 34, 90, 19}

답변 (1개)

OCDER
OCDER 2018년 8월 28일
편집: OCDER 2018년 8월 28일
T = [18 63 34 25 77 90 46 27 19];
C = cell(3);
for k = 1:length(T)
C{k} = cell(T(k), 5);
end
D = cellfun('size', C, 1);
D =
18 25 46
63 77 27
34 90 19
  댓글 수: 4
Jasmine Karim
Jasmine Karim 2018년 8월 30일
I think that's the problem, C is not holding the matrices. I used (size(iFI)) because this is part of a bigger loop and the length of iFI will keep changing.
C = cell(size(iFI));
for k = 1:length(iFI)
C{k} = cell(iFI(k), 7);
end
D = cellfun('size', C, 1);
But I get this error: Error using cell Conversion to double from cell is not possible.
And C is still full of []
OCDER
OCDER 2018년 8월 30일
iFI = num2cell(randi(5, 5)); %Is this a cell?
C = cell(size(iFI));
for k = 1:length(iFI)
C{k} = cell(iFI(k), 7);
%Can't use a cell as a index.
%cell(iFI(k), 7) == cell({#}, 7) =>
%ERROR: Conversion to double from cell is not possible.
%To fix, you need to get the integer value instead.
C{k} = cell(iFI{k}, 7); %use curly brackets {}, not parentheses ()
end
D = cellfun('size', C, 1);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by