sunny
sunny

Reputation: 51

index masking in matrix - matlab

I have matrix A and B

A = [NaN NaN 0 NaN 1 NaN NaN]
B = [0 0 1 0 0 0 0]

I want do a index masking, replace '0' with NaN by looking at matrix A index. How do script it so that my expected result would like this below.

C = [NaN NaN 1 NaN 0 NaN NaN]

Upvotes: 1

Views: 35

Answers (1)

Enlico
Enlico

Reputation: 28384

You want to put NaNs in B wherever A is NaN:

B(isnan(A)) = NaN;

This is sometimes called "logical indexing", and it's described here and here.

Upvotes: 1

Related Questions