How to delete a Matrix in a 3D Matrix?

조회 수: 11 (최근 30일)
Karoline Peters
Karoline Peters 2022년 5월 18일
답변: Steven Lord 2022년 5월 19일
Hello,
I would like to delete a Matrix in my 3D Matrix if one element in that Matrix is NaN.
Size of T5 = 4x4x30
For example if Matrix 4x4x1 looks like this:
val(:,:,1) =
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
0 0 0 1
I want to delete the whole Matrix.
My code does not work
[a b c] = size(T5)
for i = i:c
if isnan(T5(:,:,i))
T5(:,:,i) = [];
end
end
What am I doing wrong?
  댓글 수: 1
dpb
dpb 2022년 5월 18일
편집: dpb 2022년 5월 19일
if any(isnan(T5(:,:,i)))
...
See the doc on explanation of how logical if works for arrays -- in short it returns true only if all elements are true.
You can do the above w/o any looping in MATLAB with logical addressing---
ip=find(isnan(x)); % the linear indices in x containing NaN
if ~isempty(ip)
[~,~,ip]=ind2sub(size(x),ip); % any plane with at least one NaN
x(:,:,unique(ip))=[]; % remove those planes
end

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2022년 5월 18일
idx=[];
for k=1:size(T5,3)
if any(isnan(T5(:,:,k)),'all')
idx=[idx,k];
end
end
T5(:,:,idx)=[];

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 5월 18일
You are trying to change the size of the matrix that can't be achieved in this way. If you need to find and substitute NaNs with some values, then it is much better to use this approach, e.g.:
clear A
A(:,:,1) = randi([-2, 2], 5);
A(:,:,2) = randi([-2, 2], 5);
A(:,:,3) = randi([-2, 2], 5);
A=A/0;
disp(A)
(:,:,1) = NaN -Inf Inf Inf NaN Inf Inf -Inf -Inf Inf NaN NaN -Inf Inf NaN -Inf -Inf NaN Inf Inf NaN -Inf Inf -Inf -Inf (:,:,2) = -Inf Inf Inf -Inf Inf -Inf Inf NaN Inf Inf -Inf -Inf Inf NaN Inf Inf Inf Inf Inf NaN -Inf Inf Inf NaN -Inf (:,:,3) = NaN -Inf NaN NaN NaN NaN -Inf NaN Inf Inf -Inf -Inf NaN -Inf NaN Inf Inf -Inf Inf -Inf Inf Inf Inf NaN Inf
%
[R C L] = size(A)
R = 5
C = 5
L = 3
for ii = 1:L
for jj = 1:R
for kk = 1:C
if isnan(A(jj,kk,ii))
A(jj,kk,ii) = 0;
else
A(jj,kk,ii) = 13;
end
end
end
end
disp(A)
(:,:,1) = 0 13 13 13 0 13 13 13 13 13 0 0 13 13 0 13 13 0 13 13 0 13 13 13 13 (:,:,2) = 13 13 13 13 13 13 13 0 13 13 13 13 13 0 13 13 13 13 13 0 13 13 13 0 13 (:,:,3) = 0 13 0 0 0 0 13 0 13 13 13 13 0 13 0 13 13 13 13 13 13 13 13 0 13
  댓글 수: 1
dpb
dpb 2022년 5월 18일
"... trying to change the size of the matrix that can't be achieved in this way..."
Certainly can delete a plane by setting it's values to [] by logical addressing.
The problem is with the tes, not the replacementt; it only will be true if all elements of the plane are NaN so that part of the code is never executed with the sample data.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2022년 5월 19일
Let's make a sample 3-D array with some NaN values.
A = randi(10, 3, 3, 4);
A(randperm(numel(A), 3)) = NaN % Make 3 random values NaN
A =
A(:,:,1) = 1 9 6 7 NaN 7 7 3 NaN A(:,:,2) = 3 2 8 4 10 10 7 3 2 A(:,:,3) = 8 9 4 1 5 7 2 9 4 A(:,:,4) = 5 9 1 3 5 2 9 4 NaN
Identify the pages that contain NaN.
nanpages = any(isnan(A), [1 2]) % Compute any over dimensions 1 and 2
nanpages = 1×1×4 logical array
nanpages(:,:,1) = 1 nanpages(:,:,2) = 0 nanpages(:,:,3) = 0 nanpages(:,:,4) = 1
Now delete.
A(:, :, nanpages) = []
A =
A(:,:,1) = 3 2 8 4 10 10 7 3 2 A(:,:,2) = 8 9 4 1 5 7 2 9 4

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by