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

3 ビュー (過去 30 日間)
Hayao
Hayao 2018 年 4 月 13 日
コメント済み: Ameer Hamza 2018 年 5 月 2 日
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))
  2 件のコメント
Hayao
Hayao 2018 年 5 月 2 日
Wow that is awesome! Thank you very much.
Ameer Hamza
Ameer Hamza 2018 年 5 月 2 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by