How can I concatenate the last matrices of a multiple-dimensional cell array iteratively?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
In particular I want to add a new matrix (?x24) to an existing matrix (?x24) in every loop step, so that in the end there is one big matrix for each condition (1,2,3), which consists of all matrices of condition 4, i.e. X{1,2,3} = [M1{1,2,3,1}(:,1:24);M1{1,2,3,2}(:,1:24);M1{1,2,3,3}(:,1:24);....;M1{1,2,3,n}(:,1:24)] But how can be done this iteratively? This is what I have done already:
for Ind1 = 1:length(probanden)
for Ind2 = 1:length(ss)
for Ind3 = 1:length(bin)
for Ind4 = 1:length(reg)
allData{ind1,ind2,ind3} = [X{Ind1,Ind2,Ind3,Ind4}(:,1:24) Ind4]
end
end
end
end
Many thanks in advance! Lena
1 Kommentar
Jan
am 7 Feb. 2015
Bearbeitet: Jan
am 7 Feb. 2015
I've edited the question to format the code. Please use the "{} Code" button to improve the readability. Thanks.
It is not clear, if the shown code solves your needs already. If not, what is missing? In the description you use "X" and "M1", but in the code we see "allData" and "X". This is confusing.
Akzeptierte Antwort
Stephen23
am 7 Feb. 2015
Bearbeitet: Stephen23
am 7 Feb. 2015
If all of the arrays have the same number of columns, then you can do this without loops by concatenating them into one numeric array, and then splitting it apart again into a cell array. This is going to be a lot faster than concatenating the matrices in a loop. Here is an example of how to do this:
% Crate fake data, ?x2 numeric matrices in a 4D cell array:
M{1,1,1,1} = [1,2;3,4];
M{1,1,1,2} = [5,6;7,8;9,0];
M{1,1,2,1} = [Inf,NaN];
M{1,1,2,2} = [10,11;12,13;14,15];
M{1,2,1,1} = [20,21];
M{1,2,1,2} = [30,31;32,33;34,35];
M{1,2,2,1} = [40,41;42,43];
M{1,2,2,2} = [50,51;52,53;54,55;56,57];
M{2,1,1,1} = [100,101];
M{3,1,1,1} = [200,201];
% make dimension to be concatenated the first dimension:
M = permute(M,[4,1,2,3]);
% Get the matrix and cell array sizes:
X = size(M);
Y = sum(cellfun('size',M,1),1);
% Concatenate all matrices together:
Z = cat(1,M{:});
% Split this into a 3D cell array:
Z = reshape(mat2cell(Z,Y(:),size(M{1},2)),X(2:4));
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!