MATLAB Answers

How to set values to NaN when they AREN'T within the given indices

1 view (last 30 days)
Addison Collins
Addison Collins on 17 Jul 2021
Commented: Addison Collins on 17 Jul 2021
Hello,
I am attempting to set several values in a large matrix to NaN based on indices that I do not want to have NaN values. I attempted using logicals but matlab doesn't change anything (see photo). In this example, I expect every value but the bottom right to be set equal to NaN. I checked previous posts and this was how it was done in 2014.

Answers (1)

Jonas
Jonas on 17 Jul 2021
a=ones(3);
[X,Y]=meshgrid(1:3);
a(~(X==3 & Y==3))=NaN;
which equals
a(X~=3 | Y~=3)=NaN;
  4 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!