How to delete 0 elements contained in a 3D array and store them in a new 3D array

In the data stored in DividedW (3D array), in order to delete the 0 element, store the digital element in the ZERO array (3D array),I want to store the data with 0s removed in a new matrix DividedX (3D array). We are considering the program below, but as it isSince 0 is deleted, the array size is different for each Num, so an error occurs.I would appreciate it if you could teach me if there is any good way.
for Num = 1:(div1*div2)
ZERO(:,:,Num) = DividedW(:,:,Num) ~= 0;
RX(:,:,Num) = DividedX(ZERO(:,:,Num))
end
Also,
RX(:,:,Num) = DividedX(ZERO(:,:,Num))
         ↓
RX(:,:,Num) = DividedX(:,:,Num)(ZERO(:,:,Num))
I think that is the correct notation, but in this case () overlaps and an error is displayed.Is there any other notation method?

4 Comments

It is not clear to me what you want as output. Can you try to show an example? (e.g. with a 3x4x5 array)
For example, I want to remove the 0 element of such the matrix like the one below and store it in a new 3D array.
<input>
val(:,:,1) =
0 0 0 0 0 0 0
0 0 0 0.0044 0.0043 0.0041 0.0040
0 0 0 0.0045 0.0043 0.0042 0.0040
val(:,:,2) =
0 0 0 0 0 0 0
0.0031 0.0029 0.0025 0.0022 0.0017 0.0013 0.0009
0.0031 0.0028 0.0025 0.0021 0.0017 0.0012 0.0009
<output>
val(:,:,1) =
0.0044 0.0043 0.0041 0.0040
0.0045 0.0043 0.0042 0.0040
val(:,:,2) =
0.0031 0.0029 0.0025 0.0022 0.0017 0.0013 0.0009
0.0031 0.0028 0.0025 0.0021 0.0017 0.0012 0.0009
That is not possible. Each page of a Matlab array must have the same number of rows and columns. If you want this, you could use a cell array. Whether that will solve your issue depends on what you want to do next.
Thank you for your answer. Due to the specifications of my program, cell arrays cannot be used, so I think it is difficult to apply. So let's look for another way that doesn't use 3D arrays. Thank you for the place busy.

Sign in to comment.

Answers (0)

Products

Release

R2020a

Asked:

on 19 Jan 2021

Commented:

on 19 Jan 2021

Community Treasure Hunt

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

Start Hunting!