Replacing values in a matrix based on values in another matrix

조회 수: 8 (최근 30일)
Filip Hansson
Filip Hansson 2022년 8월 15일
댓글: Filip Hansson 2022년 8월 15일
If I have a matrix with "1"s and "0"s (A) and other matrixes of the same size with other values (B,C,D, etc), is it possible to find the locations of the "1"s in A in B,C,D,etc and replace these values with a constant number while the other values in B,C,D, etc are uneffected?
Thanks in advanced!

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 8월 15일
Use logical indexing (see Ch 11 of MATLAB Onramp)
A = [0 1 1 0]
A = 1×4
0 1 1 0
B = rand(size(A))
B = 1×4
0.1915 0.9248 0.8352 0.2674
B(A==1) = 99
B = 1×4
0.1915 99.0000 99.0000 0.2674

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by