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
I have a 47x47 matrix. The matrix entries consist of the values 0, 96, and 255. All entries with the value of 255 I want to transform to the value of 0. All entries with the value of 96 I want to transform to the value of 3. All entries with the value of 0 I want to transform to the value of 1. Does anyone how I could do this easily?
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
Hey! Thank you for your answer but It tells me "Error: Incorrect use of '=' opperation. To assign a value to a variable, use '='. To compare values for equality, use '=='.
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
However, now that I have this matrix A and have another matrix B (also 47x47). It doesnt let me create a matrix C=A+B. It says: 'Intergers can only be combined with integers of the same class, or scalar doubles.'
Borys Bulka
on 8 Nov 2020
I did confirm that they are both equal size with the size function. I will copy and past the code in a second.
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
Everything works as it supposed to work, expect I get an error at an_new. It doesn't calculate am_new for me.
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
It says that am_young is a class: double and am_chung is a class: uint8. Is it because of that? I am not very experienced with Matlab so I don't know all those things that are mentioned there.
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
it does calculate the am_new matrix then but the values then aren't as they supposed to be. Becuase now the values 255 and 128 are in the matrix am_new. While the am_new matrix should only contain values 0, 0.5, and 1 after adding the am_chung matrix to the am_young matrix.
Borys Bulka
on 8 Nov 2020
Edited: Borys Bulka
on 8 Nov 2020
When doing 0.5*255, the value becomes 128, but this value must remain 0.5. When doing 1*255, the value becomes 255, but this value must remain 1.
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
What does the value of 1 represent in your code? Does it mean maximum brightness of pixel? For a unit8 image, value of 1 is equivalent to 255. If you look at the values in matrix 'C'. You will see they do not vary from 0 to 1, they vary from 0 to 255.
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
I used the last code you wrote and it work! Thank you very much for your patients and help I appreciate it a lot! I am however a bit frustrated with that I do not understand why this works and my first attempt did not work. If you'd have the will still to be willing to explain that in baby steps I would highly appreciate it since I'd like to understand. Or perhaps know a link that would explain that better for me? If not, this is perfectly fine! :)
Borys Bulka
on 8 Nov 2020
So I can the only difference was to add the zeros(size()) part. Why did that change it so that I then could add them up and beforehand I couldn't?
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
Thank you very much for your time and effort! I appreciate it alot, it makes much more sense to me know!
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
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 (한국어)