Replacing values in a Matrix

조회 수: 1,122 (최근 30일)
gooniyath
gooniyath 2016년 8월 15일
답변: Stephen23 2023년 11월 5일
Hi,
I have a matrix similar to this:
A =[ ]
25 40 40 40 25 25 25 25 25 25
25 40 40 40 40 25 25 25 25 25
25 25 40 40 40 40 25 25 25 25
25 25 40 40 40 40 40 25 25 25
25 25 25 40 40 40 40 40 25 25
25 25 25 40 40 40 40 40 40 25
25 25 12 12 40 40 40 40 40 40
25 12 12 12 12 40 40 40 40 40
12 12 12 12 12 12 40 40 40 40
12 12 12 12 12 12 25 40 40 40
12 12 12 12 12 25 25 40 40 40
How do I write a script to replace all the 25's, with a certain value, and the 40's with another value and 12's with another value?
Thanks
  댓글 수: 2
Nilesh
Nilesh 2023년 11월 5일
give a single matlab commandthat will overwritethe 3rd entryin vectorB with 12
DGM
DGM 2023년 11월 5일
B(3) = 12;
If you need to resort to copy-pasting such a simple question, then good luck writing anything.

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

답변 (4개)

dpb
dpb 2016년 8월 15일
A(A==yourvalue)=NewValue;
  댓글 수: 5
Yogya Chawla
Yogya Chawla 2022년 2월 4일
can you explain this syntax
dpb
dpb 2022년 2월 4일
편집: dpb 2022년 2월 4일
A use of "logical indexing", one of the most powerful of MATLAB features, illustrated at <MatrixIndexingByLogicalExpression>.
A==yourvalue is a logical vector true where the values of A match yourvalue, false elsewhere. MATLAB then assigns the RHS to the true locations; ignoring the false positions.
It is the one way one can address an array with 0/1, but the values must be of class logical, not numeric.

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


Thorsten
Thorsten 2016년 8월 15일
편집: Thorsten 2016년 8월 15일
[Aval, ~, indAval] = unique(A);
Define the new values. Values are ordered from the smallest value to replace with to the largest, i.e., to replace 12 with 41, 25 with 26 and 40 with 13 defise Avalnew as
Avalnew = [41; 26; 13];
Anew = Avalnew(indAval);
Anew = reshape(Anew, size(A));
  댓글 수: 1
Javier Cabello
Javier Cabello 2020년 4월 17일
Much appreciated. Very flexible and smart solution!

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


BJ Anderson
BJ Anderson 2019년 3월 12일
편집: BJ Anderson 2019년 3월 12일
The real answer you're looking for is changem:
The syntax looks like this:
B = changem(A,[0 0],[9 8])
where the latter two arguments are vectors, wherein the all elements in the last vector are replaced with their counterparts in the first vector, within data array A.
Syntax
mapout = changem(Z,newcode,oldcode)
Description
mapout = changem(Z,newcode,oldcode) returns a data grid mapout identical to the input data grid, except that each element of Z with a value contained in the vector oldcode is replaced by the corresponding element of the vector newcode.
oldcode is 0 (scalar) by default, in which case newcode must be scalar. Otherwise, newcode and oldcode must be the same size.
Examples
Invent a map:
A = magic(3)
A =
8 1 6
3 5 7
4 9 2
Replace instances of 8 or 9 with 0s:
B = changem(A,[0 0],[9 8])
B =
0 1 6
3 5 7
4 0 2
  댓글 수: 1
BJ Anderson
BJ Anderson 2019년 3월 12일
A quick update on changem:
Sadly, if one inspects the actual code within changem, it functions as a loop. While it is a handy one-liner, it does not have the time-savings of moving from a looped function to an matrix-operation function.

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


Stephen23
Stephen23 2023년 11월 5일
A = [25,40,40,40,25,25,25,25,25,25; 25,40,40,40,40,25,25,25,25,25; 25,25,40,40,40,40,25,25,25,25; 25,25,40,40,40,40,40,25,25,25; 25,25,25,40,40,40,40,40,25,25; 25,25,25,40,40,40,40,40,40,25; 25,25,12,12,40,40,40,40,40,40; 25,12,12,12,12,40,40,40,40,40; 12,12,12,12,12,12,40,40,40,40; 12,12,12,12,12,12,25,40,40,40; 12,12,12,12,12,25,25,40,40,40]
A = 11×10
25 40 40 40 25 25 25 25 25 25 25 40 40 40 40 25 25 25 25 25 25 25 40 40 40 40 25 25 25 25 25 25 40 40 40 40 40 25 25 25 25 25 25 40 40 40 40 40 25 25 25 25 25 40 40 40 40 40 40 25 25 25 12 12 40 40 40 40 40 40 25 12 12 12 12 40 40 40 40 40 12 12 12 12 12 12 40 40 40 40 12 12 12 12 12 12 25 40 40 40
old = [12,25,40];
new = [99,23,42];
B = interp1(old,new,A)
B = 11×10
23 42 42 42 23 23 23 23 23 23 23 42 42 42 42 23 23 23 23 23 23 23 42 42 42 42 23 23 23 23 23 23 42 42 42 42 42 23 23 23 23 23 23 42 42 42 42 42 23 23 23 23 23 42 42 42 42 42 42 23 23 23 99 99 42 42 42 42 42 42 23 99 99 99 99 42 42 42 42 42 99 99 99 99 99 99 42 42 42 42 99 99 99 99 99 99 23 42 42 42

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by