Replacing all values of a with b in a matrix?

조회 수: 1 (최근 30일)
JARED VAHRENBERG
JARED VAHRENBERG 2020년 10월 29일
댓글: JARED VAHRENBERG 2020년 10월 29일
Say I define that a and b are 3 and 4, respectively. I heva a matrix M that I want to swap all instances of a with b. How would I do that? I tried doing
res = M(M==a) = b;
it works if I don't have the res there but I need the res there to output my result for this specific issue. It keeps telling me it needs to be
res = M(M==a) == b; but that only gives me the 2x1 logical array that has 0 0 in it
  댓글 수: 2
madhan ravi
madhan ravi 2020년 10월 29일
Show what’s M and how the resulting M should be with e a small example.
JARED VAHRENBERG
JARED VAHRENBERG 2020년 10월 29일
For example say I wanted to have matrix M= [2,5;2,8] and wanted to swap the 2's out for 1's to where then I get res = [1,5;1,8]. Why is it that my code works in the command window but as soon as I add in that res in the editor it gets angry?

댓글을 달려면 로그인하십시오.

채택된 답변

madhan ravi
madhan ravi 2020년 10월 29일
What do you expect == is a logical operator, when not used in symbolic calculations if you just want to assign it, then simply use
res = M;
res(res == a) = b

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by