delete NaN values from Matrix & use Matrix for other operations

14 ビュー (過去 30 日間)
Massimiliano Tondi
Massimiliano Tondi 2021 年 3 月 12 日
コメント済み: Massimiliano Tondi 2021 年 3 月 12 日
hey guys, I hope y'all can help me out with some awesome codes. Here is what I'm trying to do:
I have two 17759x1 double matrices, which I want to plot in order to analyze correlation. But both have some NaN values which I want to delete (shorten the matrices). In addition, one matrix sometimes has the value 9999, which I want to delete as well. I get stuck here already. The next part might be even more tricky, because the values have a time-dependency, meaning that if I delete one value(row) in one matrix I have to delete the same row(index) in the other Matrix as well, in order to plot them correctly in the end. Thanks in advance !

採用された回答

Jorg Woehl
Jorg Woehl 2021 年 3 月 12 日
% Sample arrays
A = [1; 2; 3; 4; NaN; 6; NaN; 8; 9; 10];
B = [NaN; 12; 13; NaN; 15; 16; 17; 18; 9999; 20];
We want to get rid of elements 5 and 7 (from vector A) and 1, 4, and 9 (from vector B). This can be done by first creating a logical array of the same size as A and B that contains 1 (logical true) for the elements that need to be discarded:
idx = (isnan(B) | isnan(A) | (A==9999) | (B==9999))
idx =
10×1 logical array
1
0
0
1
1
0
1
0
1
0
Once this is done, we can use idx to delete these marked elements from A and B:
A(idx) = []
B(idx) = []
A =
2
3
6
8
10
B =
12
13
16
18
20
  1 件のコメント
Massimiliano Tondi
Massimiliano Tondi 2021 年 3 月 12 日
thanks a lot! It worked perfectly !
Thank you both

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 3 月 12 日
編集済み: Walter Roberson 2021 年 3 月 12 日
discards = 9999; %could be vector of values
mask = isnan(First) | isnan(Last) | ismember(First, discards) | ismember(Second, discards);
selectedFirst = First(~mask);
selectedSecond = Second(~mask);
selectedTimes = Times(~mask);

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by