MATLAB Answers

## im uploading two codes which have different outputs. Wats the difference between these two pieces of code. I mean , why there outputs are different . Shudn't outputs be same ?

Asked by Amir

### Amir (view profile)

on 26 Feb 2015
Latest activity Commented on by sandhya

### sandhya (view profile)

on 26 Feb 2015
if true
% code
block=20*ones(200);
block1=60*ones(500);
block2=100*ones(500);
block3=150*ones(500);
block1(151:350,151:350)=block;
img1=uint8(block1);
subplot(2,3,1)
imshow(img1)
block2(151:350,151:350)=block;
img2=uint8(block2);
subplot(2,3,2)
imshow(img2)
block3(151:350,151:350)=block;
img3=uint8(block3);
subplot(2,3,3)
imshow(img3)
end
if true
% code
A=60*ones(500);
B=120*ones(500);
C=200*ones(500);
A(151:350,151:350)=20*ones(200);
subplot(2,3,1)
imshow(uint8(A))
B(151:350,151:350)=20*ones(200)
subplot(2,3,2)
imshow(uint8(A))
C(151:350,151:350)=20*ones(200)
subplot(2,3,3)
imshow(uint8(A))
end

#### 0 Comments

Sign in to comment.

## 3 Answers

### Guillaume (view profile)

Answer by Guillaume

### Guillaume (view profile)

on 26 Feb 2015

Well, in your second piece of code, you're showing A three times. If you show A, B and C you get the same figure as in the first piece:
figure;
subplot(2,3,1);
imshow(uint8(A));
subplot(2,3,2);
imshow(uint8(B));
subplot(2,3,3);
imshow(uint8(C));

#### 0 Comments

Sign in to comment.

### sandhya (view profile)

Answer by sandhya

### sandhya (view profile)

on 26 Feb 2015
Edited by sandhya

### sandhya (view profile)

on 26 Feb 2015

Rather your second code should be
if true
% code
A=60*ones(500);
B=120*ones(500);
C=200*ones(500);
A(151:350,151:350)=20*ones(200);
subplot(2,3,1)
imshow(uint8(A))
B(151:350,151:350)=20*ones(200)
subplot(2,3,2)
imshow(uint8(B))
C(151:350,151:350)=20*ones(200)
subplot(2,3,3)
imshow(uint8(C))
end

Guillaume

### Guillaume (view profile)

on 26 Feb 2015
Please use the formatting tools that this forum offers to you. In particular, the {} Code button.
Your answer is pretty much unreadable.
Amir

### Amir (view profile)

on 26 Feb 2015
Thanks to both of u for pointing out the mistake.
sandhya

### sandhya (view profile)

on 26 Feb 2015
kindly vote for my answer if it really helped you... thankyou

Sign in to comment.

### sandhya (view profile)

Answer by sandhya

### sandhya (view profile)

on 26 Feb 2015

The two codes will show different results because
In the first code you are subplotting three different images i.e block1,block2,block3
but in the second code your code subplots the same image i.e A three times...this is why you are getting wrong outputs....

#### 0 Comments

Sign in to comment.