Transforming a 3-D array into a 2-D by sorting by the 2nd layer, then removing zeros

I have made a 3-D array by iterating through 3 loops where the number of occurances in the last loop is different for each loop1/loop2.The result is a 50x10x18 array where 18 is the max value of occurances during loop 3 and if there are less occurances there will be unwanted zeros at the end of the 3rd loop (see example below)
length of loop1 = 50
length of loop2 = 10
length of loop3 = 18
example:
array(1,1, :) = [1 4 6 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0]
array(2,1, :) = [2 5 1 2 5 2 4 2 0 0 0 0 0 0 0 0 0 0]
Is there a way to iterate through the first loop (loop1) and concatenate all values from loop 3, sorted by loop 2 and then remove zero values?
I would like to end up with a 10x(18*50) array, where it is sorted by the 2nd loop then remove zeros.
for example the beginning of the first column in the 10x(18*50) array would be [1 4 6 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 2 5 1 2 5 2 4 2 0 0 0 0 0 0 0 0 0 0 ...]
then ideally after concatenation I would remove the zero values and the beginning of the first column in the array would be: [1 4 6 2 4 2 5 1 2 5 2 4 2 ...]

Answers (0)

This question is closed.

Asked:

on 25 Nov 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!