Remove sub-matrix if any col are all nan (3D matrix)

2 ビュー (過去 30 日間)
Dave
Dave 2015 年 2 月 19 日
コメント済み: Dave 2015 年 2 月 19 日
Hi, In a 3D matrix, I have 4 sub-matrices of 3x3, like A below
A=[1 2 nan; 2 5 nan; 4 2 nan];
A(:,:,2)=[3 4 5; 4 nan nan; 2 3 4];
A(:,:,3)=[8 nan nan; 1 nan nan; 8 nan nan];
A(:,:,4)=[1 2 3; 6 7 8; 3 3 5];
I need a new matrix that EXCLUDES the cases where the columns of A are ALL nan. In the case above submatrices A1 and A3 should be excluded, and the new matrix, say B, should be
B(:,:,1)=[3 4 5; 4 nan nan; 2 3 4];
B(:,:,2)=[1 2 3; 6 7 8; 3 3 5];
I'd also need the id of the sub-matrices of A to be finally used in B, in this case 2 and 4.
Let me know if it's not clear thanks

採用された回答

Image Analyst
Image Analyst 2015 年 2 月 19 日
Using the any() and all() functions I'm sure you'll be able to figure it out - you're a smart engineer. These are useful functions to learn about as they are used often. Here's one way to do it:
A=[1 2 nan; 2 5 nan; 4 2 nan];
A(:,:,2)=[3 4 5; 4 nan nan; 2 3 4];
A(:,:,3)=[8 nan nan; 1 nan nan; 8 nan nan];
A(:,:,4)=[1 2 3; 6 7 8; 3 3 5]
% Find out what slices need to be deleted.
for z = 1 : size(A, 3)
deleteThisSlice(z) = any(all(isnan(A(:,:,z)), 1))
end
B = A(:, :, ~deleteThisSlice)

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by