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

조회 수: 8(최근 30일)
Ashfaq Ahmed 2022년 7월 16일
댓글: Ashfaq Ahmed 2022년 7월 16일
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!

댓글을 달려면 로그인하십시오.

### 채택된 답변

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.
##### 댓글 수: 1표시숨기기 없음
Ashfaq Ahmed 2022년 7월 16일
Thank you sooooo much @Walter Roberson. You are a dignity saver!!!

댓글을 달려면 로그인하십시오.

### 범주

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!

Translated by