How to concatenate in a for loop to make a 3D matrix?
7 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Walter Roberson
2022-7-16
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!