**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# How to change a matrix consisting of 3 elements into another matrix of 3 different elements?

1 view (last 30 days)

Show older comments

### Accepted Answer

Ameer Hamza
on 8 Nov 2020

Edited: Ameer Hamza
on 8 Nov 2020

Try this

A; % 47x47 matrix

A_new = zeros(size(A));

A_new(A==255) = 0;

A_new(A==96) = 3;

A_new(A==0) = 1;

Read about logical indexing: https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html

##### 21 Comments

Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

Oh but it does work when I write it as a code and not in the command window!

Borys Bulka
on 8 Nov 2020

Edited: Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

am_young=zeros(size(C));

am_young(C==255)=0;

am_young(C==96)=0.5;

am_young(C==0)=1;

D=imread('cm_chung.png');

E=imresize(D, [517 517]);

am_chung=[E([6:11:512],[6:11:512])];

[r,c]=size(am_chung);

for i=1:r

for j=1:c

if am_chung(i,j)<180

am_chung(i,j)=1;

else

am_chung(i,j)=0

end

end

end

am_new=am_young+am_chung

Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

I changed the 3 to a 0.5 by the way, since I see it must be a 0.5 instead of a 3.

Borys Bulka
on 8 Nov 2020

Ameer Hamza
on 8 Nov 2020

Ok. I get it. You are mixing two things here. By default, MATLAB loads images in unit8 format in which the pixel intensities vary from 0 to 255. It appears that you want the image intensities in range 0 to 1. Although there are several ways to handle this situation, However, I recommend to do everything by considering that pixel intensity vary from 0 to 255

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

am_young=zeros(size(C));

am_young(C==255)=0;

am_young(C==96)=0.5*255;

am_young(C==0)=1*255;

am_young = uint8(am_young);

D=imread('cm_chung.png');

E=imresize(D, [517 517]);

am_chung=[E([6:11:512],[6:11:512])];

[r,c]=size(am_chung);

for i=1:r

for j=1:c

if am_chung(i,j)<180

am_chung(i,j)=255;

else

am_chung(i,j)=0

end

end

end

am_new=am_young+am_chung

Borys Bulka
on 8 Nov 2020

Edited: Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

Edited: Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

Edited: Borys Bulka
on 8 Nov 2020

When i read the images first and used different forms of thresholds and or transformation I could normally just add the two matrices up. But when using this kind of transformation:

am_young=zeros(size(C));

am_young(C==255)=0;

am_young(C==96)=0.5*255;

am_young(C==0)=1*255;

am_young = uint8(am_young);

It somehow changed the class of the matrix such that I could not add them up anymore.

Ameer Hamza
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

Edited: Borys Bulka
on 8 Nov 2020

cm_young is an image of a black grey white coloured 47x47 connectivity matrix with black, grey and white squares. I read this image in matlab and resized it such that I could take each middle pixel of each square in the 47x47 connectivity matrix image. a black pixel has in matlab a value of 0 and thus I wanted to transform every 0 to a 1. every grey pixel has a value of 96, so I want to transform that to a 0.5. Every white pixel has a value of 255, so I wanted to transform that to a value of 0. Doing this I should receive a adjacency matrix that was used to construct the original image (am_young).

So this matrix:

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

am_young=zeros(size(C));

am_young(C==255)=0;

am_young(C==96)=0.5*255;

am_young(C==0)=1*255;

am_young = uint8(am_young);

does not give me the same output when using:

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

am_young=zeros(size(C));

am_young(C==255)=0;

am_young(C==96)=0.5;

am_young(C==0)=1;

Ameer Hamza
on 8 Nov 2020

Oh! If you wanted to do that then you can directly use im2double()

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

C=im2double(C);

it will automatically scale the value between 0 and 1. Similarly, you can do the same for

am_chung=[E([6:11:512],[6:11:512])];

am_chung=im2double(am_chung);

However, it will linearly map values between 0 to 1. If you must want 96 at 0.5 then try the following code

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

am_young=zeros(size(C));

am_young(C==255)=0;

am_young(C==96)=0.5;

am_young(C==0)=1;

D=imread('cm_chung.png');

E=imresize(D, [517 517]);

am_chung=[E([6:11:512],[6:11:512])];

am_chung_ = zeros(size(am_chung));

[r,c]=size(am_chung);

for i=1:r

for j=1:c

if am_chung(i,j)<180

am_chung_(i,j)=1;

else

am_chung_(i,j)=0

end

end

end

am_new=am_young+am_chung_

Borys Bulka
on 8 Nov 2020

Borys Bulka
on 8 Nov 2020

Ameer Hamza
on 8 Nov 2020

Here is a brief explanation.

These lines

A=imread('cm_young.png');

B=imresize(A, [611 611]);

C=[B([7:13:605],[7:13:605])];

load image as uint8 matrix, in which the pixel value change from 0 to 255. Note that uint8 can only save integer values, i.e., you cannot save 0.5. But then you run the line

am_young=zeros(size(C));

By default, zeros() function contain 'double' datatype matrix, i.e., it can save fractional values, therefore, following lines work properly

am_young(C==255)=0;

am_young(C==96)=0.5;

am_young(C==0)=1;

Then again you load the image

D=imread('cm_chung.png');

E=imresize(D, [517 517]);

am_chung=[E([6:11:512],[6:11:512])];

and it again create uint8 matrix. But when you run

am_chung_ = zeros(size(am_chung));

it create am_chung_ with double datatype. am_chung_ and am_young both have double datatype, therefore, the code works properly.

Borys Bulka
on 8 Nov 2020

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)