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

6 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jul 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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!