How to delete a Matrix in a 3D Matrix?

11 vues (au cours des 30 derniers jours)
Karoline Peters
Karoline Peters le 18 Mai 2022
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 commentaire
dpb
dpb le 18 Mai 2022
Modifié(e) : dpb le 19 Mai 2022
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

Connectez-vous pour commenter.

Réponse acceptée

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

Plus de réponses (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 18 Mai 2022
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 commentaire
dpb
dpb le 18 Mai 2022
"... 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.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 19 Mai 2022
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

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by