필터 지우기
필터 지우기

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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by