Replacing values in a matrix based on values in another matrix

15 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Cris LaPierre
Cris LaPierre il 15 Ago 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

Più risposte (0)

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by