Hi,
I have a 3D array A = 90x38021x1633. The second dimension (38021) contains a lot of zeros as I have done zero padding before concatenating multiple 3D arrays to get A. How can I now remove these zeros such that B = 90xYx1633, where Y < 38021?
Thanks!

 채택된 답변

the cyclist
the cyclist 2020년 2월 3일
편집: the cyclist 2020년 2월 3일

0 개 추천

Do you mean you have "slices" of all zeros? Then this should work:
% Create an array like your A matrix, where some "slices" of data is all zeros
A = repmat([0 0 0 0; 1 0 0 0; 1 0 1 0],[1 1 5]);
% Find indices to slices in the 1-3 plane that have any non-zeros.
idx = any(A ~= 0,[1 3]);
% Create B from only the slices that have non-zeros
B = A(:,idx,:);
Just use your A, instead of the pretend one I made. Also, you can combine the two steps by just doing
B = A(:,any(A ~= 0,[1 3]),:);
but I wanted to call out explicitly what is going on.

추가 답변 (1개)

Nils Speetzen
Nils Speetzen 2020년 2월 3일

0 개 추천

Hi,
I assume you want to remove rows/planes containing only zeros. To find these, you can use
all(A==0, [1 3])
This searches for all planes where all entries along the first and third dimension are equal to zero.
To remove all those rows/planes, you can directly index A via this expression:
A(:,all(A==0, [1 3]),:) = []
I hope this helps!

댓글 수: 8

the cyclist
the cyclist 2020년 2월 3일
Uerm,
Note that Nils and my solutions are equivalent. I keep non-zeros, and he deletes zeros.
Just be careful that Nils' solution removes them from the original array A, which may be what you want, or maybe not.
Uerm
Uerm 2020년 2월 3일
편집: Uerm 2020년 2월 3일
Hi,
Thanks for the suggestions. I have tried both methods, but I end up getting the same array as the original one... I know that there are zeros, but they are not removed.
Before I got the 3D array A, I had say 10 3D arrays with size 90xAxB, where A and B were different numbers for each of these 3D arrays. I then zero padded the second dimension according to the longest one. The longest one was 38021 in length. Therefore each array ended up being 90x38021xB. Then I concatenated them all along the third axis such that the resulting 3D array has size 90x38021x1633.
Nils Speetzen
Nils Speetzen 2020년 2월 3일
Hi,
i think the resulting matrix might not have any zero-planes. With how you describe it you pad the matrices along the second axis and concatenate along the third one:
M1 =
1 0 0 4
1 2 0 4
1 2 3 0
M2 =
1 2 0 0
1 0 0 0
1 2 3 0 (last column padded)
-> concatenated:
1 0 0 4
1 2 0 4
1 2 3 0
1 2 0 0
1 0 0 0
1 2 3 0
In my example i ignored the first dimension for simplification.
Uerm
Uerm 2020년 2월 3일
Hi again,
If I for instance say length(find(stacked(1,:,1)==0)), I get the number of zeros for x = 1 and z = 1. I think this should be done for all x and z to remove all the zeros.
the cyclist
the cyclist 2020년 2월 3일
편집: the cyclist 2020년 2월 3일
Question: What does is mean to "remove the zero" from the following matrix?
M = [1 0;
2 3];
You can't do it, because there is no such thing as an empty spot or gap in a matrix. (Specifically, in an array of type double in MATLAB.) Something has to be there. There is no such thing as
M = [1 ;
2 3];
as a matrix.
What are you trying to do with the result? Could you put a NaN there instead? Or use a cell array instead of a matrix?
Uerm
Uerm 2020년 2월 3일
Hi, the point with the zero padding is to concatenate the 3D arrays into one 3D array. I want to remove these zeros after I have concatenated the arrays. The reason for this is that I will use this 3D array to calculate some features which are to be used for classification (machine learning). These added zeros will therefore have an influence on the calculated features.
Will it be a better idea to have the second dimension be equal to the shortest one for the 3D arrays before I concatenate them?
I'm not certain I fully understand, but I am getting some idea. Again, looking at just some small arrays as examples.
So maybe you had an array A1:
A1 = [1 2 0;
3 4 5];
where that zero was added for padding.
And you have another array A2:
A2 = [5 6 7;
8 9 0];
where that zero was also added for padding.
And you end up with A by concatenating A1 and A2 along the third dimension:
A = cat(3,A1,A2)
A(:,:,1) =
1 2 0
3 4 5
A(:,:,2) =
5 6 7
8 9 0
So A has some data where the zeros are not meaningful, and are just placeholders.
But, repeating my prior comment, you CANNOT just "remove" them. A matrix cannot have an "empty" spot.
You could do
A(A==0) = NaN
A(:,:,1) =
1 2 NaN
3 4 5
A(:,:,2) =
5 6 7
8 9 NaN
which replaces the zeros with NaN (not-a-number). You might then be able to do later steps in your calculation. Does that help?
Uerm
Uerm 2020년 2월 10일
Thanks a lot for your help! It makes sense now.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 2월 3일

댓글:

2020년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by