Replace elements of a matrix

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2019-1-29
编辑:Stephen23 2019-1-29
"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).
klb
klb 2019-1-30
编辑:klb 2019-1-30
Thanks Stephen. It was not a repost. Do read, i state upfront, with a link. To save everyone the effort and mostly time. Exactly as you say i should.
I am not here because answers are free. I am seeking the expertise of the form, and you are also gaining in some form hence you volunteer. You looked up I posted 7 questions in 3 months? Did you read through all of them? Bet you didn’t. As then you’d see I individually thank and give positive words to EACH ONE. And if question was edited, I explained myself. MATLAB is coding - there are multiple ways to achieve an answer. And no offense, but sometimes my questions were only half read/understood and answered – and I had to re-ask and the someone did the same thing you have. And the question was flagged, marked duplicate all hell broke loose on my post on the forum, including people taking it as opportunity to post disrespectful comments and deleting it few minutes later, without appreciating that notification come to my emails too and still there. And I can take them to the moderators. But I’m just like - wow!
What you can’t know is that I ran the answer code, but that lead to some error messages i.e. a point which needed fixing. So I looked up MATLAB Help section couldn’t isolate error was, I tried few iterations on my own and that didn’t work, then checked other non-MathWorks MATLAB forums for answer, didn’t see anything similar that resolves my issue, then came back here and asked the person who replied to my questions that the code doesn’t work with decimals could you give an update. I do this each time before I have re-asking. This time, I didn’t get response, so I posted the question fresh, updated to include my learnings through running the code - FYI, reposting has been suggested to me by another one of volunteers of this form, at a separate another post that a repost is better than asking with edits/updates as that confuses. And I agree.
I am here to learn and understand, and I never copy-paste anyone's work just to get moving on mine. And I respect to those who respond, more so because it is indeed on his/her own time. I don’t appreciate you selecting half my statement, ignoring the other half and give me “a word”. My comment on asking for refrain comes from my personal experience as explained above (not yours) and to me is legit - And take note that right now, I am gving you too this respect by explaing my raitonale for my statement. Regardless, you have marked tmy new-posted question as duplicate, which it is not, and now no one will answer that so I have had to taken down and now I won't have an answer. Thank you very much. My 2 days lost. Hope that makes your day?

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2019-1-28
编辑:Andrei Bobrov 2019-1-29
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 个评论
klb
klb 2019-1-29
Thanks Andrei. Some of the numbers are decimals and after 90 rows, the values chage hence a referenced logic would be requried.
Luna
Luna 2019-1-29
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??

请先登录,再进行评论。

更多回答(2 个)

madhan ravi
madhan ravi 2019-1-28
编辑:madhan ravi 2019-1-28
By logical indexing:
A(A==4)=10 % likewise follow the same way for the rest
  1 个评论
klb
klb 2019-1-29
编辑:klb 2019-1-30
thanks madhan. I know this method but it is hardwrired appraoch. I was inherently expecting a reference based logic when i posted quesiton. that will work regardless of numbers. I have reposted this quesiton as new one. Much thanks for the links to the blog.

请先登录,再进行评论。


Steven Lord
Steven Lord 2019-1-28
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 个评论
Andrei Bobrov
Andrei Bobrov 2019-1-29
Please see part 'ADD' in my answer.
klb
klb 2019-1-30
That works. Thank you so Andrei!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by