how to discard NaN vectors from a 3D matrix

1 回表示 (過去 30 日間)
Itzik Ben Shabat
Itzik Ben Shabat 2013 年 11 月 20 日
回答済み: ag 2024 年 12 月 27 日
Hi, I have a 3D matrix. some of its rows/columns/depth vectors can be all NaN. I wish to creat a new matrix which is the same as the original one only without the NaNs. is there a function that can help me do this ? in 2D i used ismember and just substitute them with []. but for the 3D case im stuck. here is an example code of such matrix
mat=ones(6,6,6);
mat(1:2,:,:)=NaN(2,6,6);
NANLoc=isnan(mat);
( so I want mat(3:6,:,:) as a new matrix)
any ideas? thanks

回答 (1 件)

ag
ag 2024 年 12 月 27 日
Hi Itzik,
I understand that you are want to remove all slices in your 3D matrix that contain NaN values. You can achieve this by utilizing the "isnan" function in MATLAB.
Below is a code snippet that demonstrates how to accomplish this:
% Identify slices to keep (those without any NaN values)
slicesToKeep = ~any(any(isnan(M), 1), 2);
% Construct the new matrix by retaining only the non-NaN slices
newM = M(:, :, slicesToKeep);
For more details, please refer to the following MathWorks documentation:
Hope this helps!

カテゴリ

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