Replace values in a matrix
Show older comments
Hi,
I have a 250x250 matrix, each cell having either 1,2,3,4,5 or NaN, derived from kmeans index. I'm looking to reorder my kmeans values and have found a new order for these values based on populatation e.g. 3 4 5 2 1, 3 being most populated, 1 being least. I'm looking for a way to tell the system to make 3 the new 1, 4 the new 2 etc.. i.e. replace all 3s with 1, all 4s with 2, all 5s with 3, all 2s with 4, all 1s with 5. However, when I use a loop, I end up losing data as it thinks the converted numbers were my original ones and replaces them to a new value.
2 Comments
KALYAN ACHARJYA
on 14 Dec 2020
"I end up losing data as it thinks the converted numbers were my original ones and replaces them to a new value"
Copy the data with temp variable. The initial part of the question is not clear to me. Please make it easy to understand with simple examples.
KF
on 14 Dec 2020
Accepted Answer
More Answers (1)
David Hill
on 14 Dec 2020
a=A==1;
b=A==2;
A(A==3)=1;
A(A==4)=2;
A(A==5)=3;
A(b)=4;
A(a)=5;
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!