How to concatenate in a for loop to make a 3D matrix?

14 vues (au cours des 30 derniers jours)
Ashfaq Ahmed
Ashfaq Ahmed le 16 Juil 2022
Commenté : Ashfaq Ahmed le 16 Juil 2022
Dear altruists,
I have this Saved_Temp{} that has 10x1 cells. Each of the cell is 1130x800 double. I want to create a 3D cell that will concatenate all these 10 cells and the ultimate matrix should look like 1130x800x10 size. I am using this code, but it is not working -
TEMP_2013 = [];
%change
for i = 1:length(Saved_Temp)
TEMP_2013{i} = cat(3,Saved_Temp{i});
end
However, it actually works if I do it manually -
TEMP_2013 = cat(3,Saved_Temp{1},Saved_Temp{2},Saved_Temp{3},Saved_Temp{4},...
Saved_Temp{5},Saved_Temp{6},Saved_Temp{7},Saved_Temp{8},Saved_Temp{9},...
Saved_Temp{10});
Can anyone poease give me an idea on how can I do it? Because soon I will have to work with large number of cells and I can not manually do it.
Any feedback from you will be much appreciated!

Réponse acceptée

Walter Roberson
Walter Roberson le 16 Juil 2022
Temp_2013 = cat(3, Saved_Temp{:}); %no loop
If you must use a for loop then
Temp_2013 = [];
for i = 1:length(Saved_Temp)
TEMP_2013 = cat(3,Temp_2013, Saved_Temp{i});
end
Another approach is
Temp_2013 = cell2mat( reshape(Saved_Temp, 1, 1, []) );
which re-arranges the 10 x 1 cell into 1 x 1 x 10 cell and then uses cell2mat on the result. But the method I showed first is more efficient.
  1 commentaire
Ashfaq Ahmed
Ashfaq Ahmed le 16 Juil 2022
Thank you sooooo much @Walter Roberson. You are a dignity saver!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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