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

30 ビュー (過去 30 日間)
Addison Collins
Addison Collins 2021 年 7 月 17 日
コメント済み: Addison Collins 2021 年 7 月 17 日
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.

回答 (1 件)

Jonas
Jonas 2021 年 7 月 17 日
a=ones(3);
[X,Y]=meshgrid(1:3);
a(~(X==3 & Y==3))=NaN;
which equals
a(X~=3 | Y~=3)=NaN;
  4 件のコメント
Simon Chan
Simon Chan 2021 年 7 月 17 日
If there is an original matrix contains different values and you want to keep part of it, you may try the following:
DataKeep = matrix_original(yind(1):yind(end),xind(1):xind(end),:); % Keep the data from the original matrix
[n,m,k] = size(matrix_original); % k should be 1000 in your case
matrix=nan(n,m,k); % Create a new matrix with NaN only
matrix(yind(1):yind(end),xind(1):xind(end),:)=DataKeep; % Put the data you want to keep in the new matrix
Addison Collins
Addison Collins 2021 年 7 月 17 日
@Simon ChanThat solution worked well. Sorry to both of ya for the poor explanation.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by