How to merge matrix in a loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Akshay Sahu
am 28 Okt. 2022
Kommentiert: Akshay Sahu
am 29 Okt. 2022
Suppose I have four matrix
A,B,C and D. These all are (mx3) matrix where m is some number.
and I have to define a number as an user input which is N.
Now I want a merge matrix such that,
for N = 1
New_matrix = [A]
for N = 2
New_matrix = [A
B
C]
for N = 3
New_matrix = [A
B
C
D
A]
for N = 4
New_matrix = [A
B
C
D
A
B
C]
and so on......
0 Kommentare
Akzeptierte Antwort
David Hill
am 28 Okt. 2022
A=randi(10,4,3)
B=randi(10,5,3)
C=randi(10,6,3)
D=randi(10,7,3)
N=2;
newMatrix=generateMatrix(A,B,C,D,N)
function newMatrix=generateMatrix(A,B,C,D,N)
n=2*N-1;
s=[size(A,1),size(B,1),size(C,1),size(D,1)];
newMatrix=repmat([A;B;C;D],ceil(n/4),1);
S=sum(s*floor(n/4))+sum(s(1:mod(n,4)));
newMatrix=newMatrix(1:S,:);
end
Weitere Antworten (1)
KALYAN ACHARJYA
am 28 Okt. 2022
Bearbeitet: KALYAN ACHARJYA
am 28 Okt. 2022
% Just example
A=rand(4,3);
B=rand(4,3);
C=rand(4,3);
D=rand(4,3);
data_mat={A,B,C,D}; % Created to handle data easily
N=4 ; % Example or length of data_mat
data=cell(1,N); %Memory Pre-allocation
for i=1:N
data{i}=horzcat(data_mat{1:i});
end
data
You may look for cellfun also, please do the modification as per requirements.
2 Kommentare
KALYAN ACHARJYA
am 28 Okt. 2022
To store matrix or any other data type, cell array is much easier way to handle it.
Hope it helps!
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!