# m1(m2 == 1), but with dimensions of m1 intact?

1 Ansicht (letzte 30 Tage)
Ulrik William Nash am 23 Aug. 2017
I have a matrix of 1's and 0'1, m2. I have another matrix with the same dimensions as m2, containing values (i.e., m1). Of these values, I am only interested in those where the corresponding value in m2 is 1. However, as an extra condition, I wish to create m3, which has the same dimensions as m1 and m2, and contains the selected values in the corresponding places, with all other places being NaN. I am stuck on how to do that without looping.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 23 Aug. 2017
Bearbeitet: Stephen23 am 23 Aug. 2017
Method one:
m3 = m1;
m3(m2~=1) = NaN;
Method two:
m3 = NaN(size(m1));
idx = m2==1;
m3(idx) = m1(idx);
Method three :
m3 = m2./m2.*m1;
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Ulrik William Nash am 23 Aug. 2017
Method 3 is very neat. Thank you.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by