How to loop through cell arrays

조회 수: 23 (최근 30일)
Dan Page
Dan Page 2018년 11월 13일
댓글: Nurul Ain Basirah Zakaria 2020년 9월 28일
I have a 5x5 cell array, called H, that I am trying to loop through and do an equation on each cell. How would I do this?
I am using mat2cell to split a larger image,F2, into the 5x5 cell array.
H = mat2cell(F2, [40 40 40 40 40], [48 48 48 48 48])
To access the first part of the cell array I use H{1,1} but do not know how to automaticaly move onto H{1,2} until H{1,5} then move to H{2,1}.
I hope this makes sense.
  댓글 수: 1
Mark Sherstan
Mark Sherstan 2018년 11월 13일
Does the following push you in the correct direction?
for ii = 1:5
for jj = 1:5
fprintf('%0.f %0.f\n',ii,jj)
end
end

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

답변 (2개)

Stephen23
Stephen23 2018년 11월 14일
S = size(H);
for rx = 1:S(1)
for cx = 1:S(2)
H{rx,cx}
end
end

possibility
possibility 2018년 11월 14일
Hi,
First, you don't need to use cell array as your dimensions are all same. 40x48.
If you still want to work with cell array, you may try following:
h11=H{1,1}(:,:);
which converts the cell array inputs into matrix form. And then you do your equation on h11. General case,
for i=1: size(H,1) %scan rows
for j=1:size(H,2) %scan columns
hij=H{i,j}(:,:); %convert matrix form
hij_manipulated= equation (hij); %do the equation
H(i,j)={hij_manipulated}; %put it back to its original location in cell array
end
  댓글 수: 1
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2020년 9월 28일
hello. can i use this equation for 444x200x400 double?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by