how to write for loop with two variables

4 views (last 30 days)
Hello, I'm having problem with this it of code:
for j = 1:3
for m = 3:-1:1
imagesc(images{m}, 'XData', [fid1(j,1) (size(Image1,2) + fid1(j,1))], 'YData',[fid2(j,1) (size(Image1, 1) + fid2(j,1))]), axis equal, axis xy
alpha(0.7)
hold on
end
end
As you can see, I want the images{m} to start from 3 and end at 1, while the fid1(j,1) commands to go in the opposite direction. Any ideas how to solve this? One idea I had was to flip my images{m} array but I'm not sure how to do this
Thanks

Accepted Answer

Image Analyst
Image Analyst on 22 Jul 2014
Try
images = fliplr(images); % or flipud() - what ever works.
Or
images = images(end:-1:1);
  3 Comments
Joseph Cheng
Joseph Cheng on 22 Jul 2014
Edited: Joseph Cheng on 22 Jul 2014
*cough* well if you take a look at the answer below. you can see how to get 2 variables to work. why your nested (is that the right term?) for loop doesn't really do what you want is because it'll do all combination of m for each j. so for j=1 you'll run through it for m=3,2,1 and then for j=2 you'll run through it again for m=3,2,1 and so on.
Joseph Cheng
Joseph Cheng on 22 Jul 2014
Edited: Joseph Cheng on 22 Jul 2014
Oh and additionally thinking about it more you could have had
m=length(images);
for j = 1:3
imagesc(images{m+1-j}, 'XData', [fid1(j,1) (size(Image1,2) + fid1(j,1))], 'YData',[fid2(j,1) (size(Image1, 1) + fid2(j,1))])
axis equal
axis xy
alpha(0.7)
hold on
end

Sign in to comment.

More Answers (1)

Joseph Cheng
Joseph Cheng on 22 Jul 2014
Edited: Joseph Cheng on 22 Jul 2014
make an array called index = 3:-1:1;
for j = 1:3
m=index(j);
imagesc(images{m}, 'XData', [fid1(j,1) (size(Image1,2) + fid1(j,1))], 'YData',[fid2(j,1) (size(Image1, 1) + fid2(j,1))])
axis equal
axis xy
alpha(0.7)
hold on
end

Community Treasure Hunt

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

Start Hunting!