Concatenating a 3D matrix in the 3rd dimension when some cells are empty

I have a 3D cell array 'A' of size 120 x 180 x 365 (latitude, longitude, day of year).
If I call up a single cell (e.g. A(1,1,1)), this opens matrix 'B' which has two columns (vessel identity, speed), but the number of rows varies by cell. Some cells do not contain any data and display [ ] instead of a B matrix.
I need to make one B matrix that displays all the vessel identities and speeds for A(1,1,1:365)
How can I vertically concatenate matrix B in the third dimension?
Thank you for your help.

 Accepted Answer

B = cat(1,A{1,1,1,:}) % N-by-2 array

7 Comments

Thank you! Your answer results in a 1x18 cell array where each cell looks like this: [vessel_identity, vessel_speed]. Do you know how I could transform this into a 2x18 matrix with columns vessel_id and vessel_speed?
What is A{1,1,1} ? A cell, or a M-by-2 (numerical) matrix?
The only way that cat(1,A{1,1,1,:}) could have resulted in a 1 x 18 cell array was if A{1,1,1,1} is a 1 x 18 cell array and all other entries in A{1,1,1,:} either do not exist or are empty. It looks like perhaps the arrays is 3D instead of 4D, so A{1,1,1,:} would probably be the same as A{1,1,1} anyhow. Could you confirm that A{1,1,1} is a 1 x 18 cell array? That would be inconsistent with what you said about A(1,1,1) earlier, by which you probably meant A{1,1,1} .
If you had said that cat(1,A{1,1,1,:}) had resulted in an 18 x 1 cell array then that would have been more understandable, especially if we guess that you used cat(1,A{1,1,:}) instead of cat(1,A{1,1,1,:}) . But the only way to be consistent about having gotten a 1 x 18 cell array would be if you had used cat(2,A{1,1,:}) or some other instruction completely.
I just verified, and A{1,1,1} results in a 1 x 18 cell array.
B = cat(1,A{1,1,1,:}), suggested by Jos, results in the same 1 x 18 cell array.
cat(1,A{1,1,:}) results in this error:
Error using cat
Dimensions of matrices being concatenated are not consistent.
You said that opening up a single element A(1,1,1) results in a B matrix with two columns. But A{1,1,1} is returning a 1 x 18 cell array. There is a contradiction here.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!