Convert 3D cell array (with 3D vector in each cell) into a single matrix

3 views (last 30 days)
I have a n-by-n-by-n cell array, where each cell contains a vector of 3D coordinate. I want to convert this 3D cell array into a single n^3-by-3 2D matrix.
For example, suppose I have 3x3x3 cell array with each cell containing a 3D vector (with coordinate [x y z]). I want to make a matrix that looks like this:
[x1 y1 z1]
[x2 y2 z2]
...
[x27 y27 z27]
Of course I will not be able to use cell2mat command for this. So my intuition was to concatenate using for loop. Let's say C is the 3x3x3 cell array containing 3D vectors in each cell:
M = zeros(0,3)
for c = 1:3
for b = 1:3
for a = 1:3
M = cat(1,M,C(a,b,c))
end
end
end
Is there a more straightforward way to do this? Because this seems like a inefficient method to use, especially when I am planning to extend it to n-by-n-by-n 3D cell array. Or is this the only way to do this?
Note: Searching the MATLAB answers only yielded results that were specialized to each of the question and was not applicable for my situation.
Thank you

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 May 2018
Try this
output = cell2mat(reshape(yourCellMat, [], 1))

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!