How can I use a matrix to be a loop variable

For example, in usual for loop, it looks like that
for i=1:1:10
disp(i)
end
if I want the variable to be a matrix,like:
for mat = set of matrices
imshow(mat);
end
the set of matrices is composed of {mat1,mat2,mat3,mat4}

댓글 수: 1

I don't believe it's possible for a for loop index to be larger than a 1x1 size. That doesn't mean though that it's not possible to use the matrices within your loop. How are your matrices related? What type of organization do you have for them?

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

 채택된 답변

Honglei Chen
Honglei Chen 2019년 3월 1일

0 개 추천

If you have somethign like
mat = {mat1,mat2,mat3,mat4}
You can do
for m = 1:numel(mat)
imshow(mat{m})
end
HTH

댓글 수: 3

Wow, thank you very much, this is just so convenient.
Also, I found another method, like
matlis=mat1;
matlis(:,:,2)=mat2;
matlis(:,:,3)=mat3;
for i=1:3
figure
imshow(malis(:,:,i))
end
but this is not as concise as your solutionm, thanks again!
You can also do
for m = {mat1, mat2, mat3, mat4}
figure()
imshow(m{1})
end
Ziyu Zhao
Ziyu Zhao 2019년 3월 2일
yes, thanks a lot, this kind of structure is interesting, and very useful.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 3월 1일

댓글:

2019년 3월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by