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

1 view (last 30 days)
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
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;
  21 Comments
Ameer Hamza
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
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!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!