Replacing values in a matrix based on values in another matrix

15 views (last 30 days)
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!

Accepted Answer

Cris LaPierre
Cris LaPierre on 15 Aug 2022
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

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices 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!