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

8 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Walter Roberson
Walter Roberson il 16 Lug 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.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by