ignore NaN values in loop

31 ビュー (過去 30 日間)
Cristina Elmeua
Cristina Elmeua 2021 年 1 月 12 日
コメント済み: Cristina Elmeua 2021 年 1 月 16 日
Hi everyone,
So I have a numeric matrix "walknov" with several columns of different lengths and I interpolated them all to be 100 points and created a new matrix "CC" with the following loop:
for i = 2:16
A = walknov(:,i);
A(any(isnan(A),2),:) = [];
A = interpft(A, 100);
CC = [CC A];
end
NaNs fill the differences of length between columns, so I have eliminated each row containing NaN everytime I interpolate each column. The problem is that some columns have no values at all (i.e. only NaNs) and this makes the loop stop as the interpolation function won't accept such values.
I do not want to drop the column as I need the matrix to match other matrices, but instead I would like to just leave NaN values in those missing columns. Is there a way to solve this?
Thanks a lot!
PS: I have attached a sample dataset

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 12 日
編集済み: Walter Roberson 2021 年 1 月 12 日
if isempty(A)
CC(:,i) = nan;
else
A = interpft(A, 100);
CC(:,i) = A;
end
Can interpft operate with only one non-nan value?
  1 件のコメント
Cristina Elmeua
Cristina Elmeua 2021 年 1 月 16 日
This has worked great Walter. A very simple and effective approach, thanks.

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

その他の回答 (2 件)

David Hill
David Hill 2021 年 1 月 12 日
CC=[];
for i = 2:16
A = walknov(:,i);
A = interpft(A(~isnan(A)), 100);
if isempty(A)
CC=[CC;nan(1,100)];
else
CC = [CC;A];
end
end
  1 件のコメント
Cristina Elmeua
Cristina Elmeua 2021 年 1 月 16 日
Thanks for the help David!

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


Adam Danz
Adam Danz 2021 年 1 月 12 日
The image below shows the location of your missing values. The good news is that the missing values either consume entire columns or the end of columns instead of being dispersed.
To work around this,
for i = 2:16
A = walknov(:,i);
A(any(isnan(A),2),:) = [];
if isempty(A)
A = NaN(size(CC,1)); % 1 column?
else
A = interpft(A, 100);
end
CC = [CC A];
end
This assumes CC is defined before the loop. Otherwise if the empty 'A' appears on the first loop, there will be an error indicating that CC is not defined.
Show NaN pattern.
imagesc(isnan(w2_2)) % or heatmap(double(isnan(w2_2))
  1 件のコメント
Cristina Elmeua
Cristina Elmeua 2021 年 1 月 16 日
Thanks for the help Adam!

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by