フィルターのクリア

Find the indices of one variable in another one avoiding NaNs

2 ビュー (過去 30 日間)
Daria Ivanchenko
Daria Ivanchenko 2020 年 11 月 3 日
コメント済み: Daria Ivanchenko 2020 年 11 月 4 日
Hi!
I have two matrices, A and B, they have the same number of rows and different number of columns. Matrix A is just some numbers, matrix B is the indices. Both matrices have different amout of NaNs in the end.
I want to find the number in matrix A that matches the index in the matrix B (using the simple C = B(A)). But it doesn't work with NaNs inside the matrices. Is there some other way to do it?
A = [1 2 3 7 8; 1 5 3 NaN NaN; 1 9 3 6 NaN];
B = [2 4 5 NaN; 1 2 3 NaN NaN; 1 2 NaN NaN NaN];
C = B(A);
C = [2 7 8; 1 5 3; 1 9];
Thanks!
  4 件のコメント
Daria Ivanchenko
Daria Ivanchenko 2020 年 11 月 3 日
Regarding the sample, this one is correct:
A = [1 2 3 7 8; 1 5 3 NaN NaN; 1 9 3 6 NaN];
B = [2 4 5 NaN NaN; 1 2 3 NaN NaN; 1 2 NaN NaN NaN];
C = B(A);
C = [2 7 8; 1 5 3; 1 9];
Yeah, I missed one NaN, sorry.
Stephen23
Stephen23 2020 年 11 月 3 日
編集済み: Stephen23 2020 年 11 月 3 日
This is not a valid matrix, because the rows have different numbers of elements:
C = [2 7 8; 1 5 3; 1 9]; % NOT VALID

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

採用された回答

Stephen23
Stephen23 2020 年 11 月 3 日
編集済み: Stephen23 2020 年 11 月 3 日
The C you showed in your example is not possible as it has different numbers of elements in each row, but you could put the data into a cell array:
>> A = [1 2 3 7 8; 1 5 3 NaN NaN; 1 9 3 6 NaN]
A =
1 2 3 7 8
1 5 3 NaN NaN
1 9 3 6 NaN
>> B = [2 4 5 NaN NaN; 1 2 3 NaN NaN; 1 2 NaN NaN NaN]
B =
2 4 5 NaN NaN
1 2 3 NaN NaN
1 2 NaN NaN NaN
>> idx = ~isnan(B);
>> [idr,~] = find(idx);
>> idc = B(idx);
>> idy = sub2ind(size(A),idr,idc);
>> C = accumarray(idr,A(idy),[],@(v){v});
>> C{:}
ans =
2
7
8
ans =
1
5
3
ans =
1
9

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by