subplot two different images in two different for loop

2 次查看(过去 30 天)
I need to display two images suppose as in my case it is patch 1 (P1) and patch2 (P1*) as simultaneous subplots like using for loop
again after incrementing the for loop the upcoming two images should come as the next subplots
But iam getting all P1 to P20 first then P*1 is coming as P*21 which is not what i ought to do as shown below:
I need first P1 and P*1
Here is the code:
% Plotting the patches
figure;
k=1;
for i = 1 : 40 : rowsI-40
for j = 1 : 40 : columnsI-40
im_subL=I(i:i+39,j:j+39);
im_subR=imr(i:i+39,j:j+39);
subplot(round(rowsI/40),round(columnsI/40),k),imshow(im_subL);
title(['P',num2str(k)])
subplot(round(rowsR/40),round(columnsR/40),k+1),imshow(im_subR);
title(['P*',num2str(k+1)])
k=k+1;
end
end
I need to correct it could anybody please suggest me what to do?

采纳的回答

Rik
Rik 2019-6-29
编辑:Rik 2019-6-29
You are overwriting your plots. If you step through your code you will see that you first make an k+1 subplot, then increment k by one and on the next iteration you overwrite the k+1 plot.
If you change k=k+1 to k=k+2 the result should be closer to what you expect.
Edit:
Something like the code below should make it a lot clearer and should be easier to edit to what you want.
% Plotting the patches
figure;
sub=struct;
sub.rows=round(rowsI/40);sub.cols=round(columnsI/40);
sub.k=1;k=1;
for i = 1 : 40 : rowsI-40
for j = 1 : 40 : columnsI-40
im_subL=I(i:i+39,j:j+39);
im_subR=imr(i:i+39,j:j+39);
subplot(sub.rows,sub.cols,sub.k);sub.k=sub.k+1
imshow(im_subL);title(['P',num2str(k)])
subplot(sub.rows,sub.cols,sub.k);sub.k=sub.k+1
imshow(im_subR);title(['P*',num2str(k)])
k=k+1;
end
end
  4 个评论
Rik
Rik 2019-6-29
Then you need to make sure you edit the code that determines the number of rows and column of subplots so that you allocate enough space for all your images.
Pravita Lekshmanan
Pravita Lekshmanan 2019-6-29
Could you please tell me how can i accomodate all images
by not increasing the size of patches from 40x40 to some higher patch dimension

请先登录,再进行评论。

更多回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-6-29
P*1 is coming as P*21 which is not what i ought to do?
As the title is displayed based on k value, as it reflects the number of iteration, as you have defined k=1
and within the loop
k=k+1;
When the code has to be displayed P*1 again, you may look for another variable, say l, as similar as k, for lower parts plots. (* displayed, I am supposing it exucated the lower title)

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by