Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Hayao
Hayao le 13 Avr 2018
Commenté : Ameer Hamza le 2 Mai 2018
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

Réponse acceptée

Ameer Hamza
Ameer Hamza le 1 Mai 2018
Try this
output = cell2mat(reshape(yourCellMat, [], 1))
  2 commentaires
Hayao
Hayao le 2 Mai 2018
Wow that is awesome! Thank you very much.
Ameer Hamza
Ameer Hamza le 2 Mai 2018
You are welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by