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

3 次查看(过去 30 天)
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

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by