Replace elements of a matrix
6 views (last 30 days)
Show older comments
UPDATE: I expanded and updated the question and posted it as a new one. here : https://uk.mathworks.com/matlabcentral/answers/442116-match-and-replace-matrix-elements
To everyone else: If you can chill out on telling me with a link to this post that "ooh look you already asked it", you will help keep off clutter in the answer window while I await an answer.
A = [1 4 3; 2 3 2;4 2 1....] (this is a 90 row matrix)
How do I make the follwing replacemetns in A using Logicals
4 with10
3 with 15
2 with 20
1 with 30
2 Comments
Stephen23
on 29 Jan 2019
Edited: Stephen23
on 29 Jan 2019
"If you can resist telling me with a link to this post that "ooh look you already asked it", you will help keep off clutter in the answer window while I await an answer. "
Actually those links help you. When you post basically the same question multiple times it makes it harder for us to keep track of what information you have given us, and what information you have been given. Giving links to your earlier related questions reduces the likelihood that you will be given exactly the same answers again, and reduces the frustration that you will cause the volunteers who spend time writing an answer and then you reply to their efforts "sorry, but I got that answer in an earlier question". Those links help us, which means they help you too.
Of course, even better is not posting the same question multiple times.
Instead of telling us how this forum works (after just three months active and seven questions), perhaps you could focus on writing clear, well explained questions (which are more likely to get you the help that you want).
Accepted Answer
Andrei Bobrov
on 28 Jan 2019
Edited: Andrei Bobrov
on 29 Jan 2019
A = [1 4 3; 2 3 2;4 2 1;10,7,1];
b = [4 ,10
3 , 15
2 ,20
1 , 30];
[lo,ii] = ismember(A,b(:,1));
A(lo) = b(ii(lo),2);
ADD:
A = [0.22, 0.3, 0.456; 0.3, 0.456,0.456;.22,.526,.7];
b = [0.22,93.55; 0.30, 91.05; 0.456,89.58 ];
A = A + eps(100)*(rand(size(A))-.5)*2;
solution:
[lo,ii] = ismembertol(A,b(:,1),eps(1e3),'DataScale',1);
A(lo) = b(ii(lo),2);
2 Comments
Luna
on 29 Jan 2019
First you need to explain in details that how your matrix and values change? How do you you store your replacement matrix if it changes?
Given answer of Andrei above looks very well to me.
Why this does not work for your case you need explain it in details with a an example or something. Because we really didn't understand.
You can change your A and b in a cell array then again apply the same logic in a for loop for example??
More Answers (2)
madhan ravi
on 28 Jan 2019
Edited: madhan ravi
on 28 Jan 2019
By logical indexing:
A(A==4)=10 % likewise follow the same way for the rest
Read https://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/ - a blog by Loren Shure , it’s interesting.
Steven Lord
on 28 Jan 2019
Logical indexing is one approach. Two others are (if A contains only positive integer values) linear indexing:
A = randi(4, 10, 3) % Sample data
x = [10 15 20 30];
B = x(A)
or the discretize function:
C = discretize(A, 1:5, x)
3 Comments
See Also
Categories
Find more on Historical Contests 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!