How to save matrix created in loop
Afficher commentaires plus anciens
I am running the below code which gives me (5,4) matrix output (generated by B matrix) for each "i"
I want to save the output (generated by B matrix) from each "i" one after one. Which means after the 2nd "i" I will have (10, 4) matrix and for 3rd "i" I will have (15, 4) matrix and so on. Final output should be of (length(i)*5, 4) matrix.
Please help. See the code below
A=xlsread('Jan.xlsx','sheet1','B3:E2000');
n=size(A,1);
for k=5;
for i=1:k:n-(k-1);
a=A(i:i+(k-1),:);
B=a(randperm(k),:);
end
end
Réponses (2)
A = xlsread('Jan.xlsx','sheet1','B3:E2000');
n = size(A, 1);
k = 5; % ??? Without a FOR
index = 1:k:n-(k-1);
len = length(index);
B = cell(1, len);
for c = 1:len % EDITED: "i" -> "1"
i = index(c);
a = A(i:i+(k-1), :);
B{c} = a(randperm(k), :);
end
3 commentaires
Swaami Jan
le 6 Mai 2016
Jan
le 6 Mai 2016
This was a typo. See EDITED.
Karanvir singh Sohal
le 1 Mar 2021
I am also facing such problem to save a multiple matrixs from loop in a single matrix
X=[300; 400; 600];
[m,n]=size(X);
for i=1:m
Y=[1.5*X(i);2*X(i)];
[mY,nY]=size(Y);
for j=1:mY
Mat(j,1)=X(i);
end
Mat(:,2)=Y
end
At the end I want a single matrix "c" that can combine all the resultant matrics from the loop
Expected Result
c= [ 300 450; 300 600; 400 600; 400 800; 600 900; 600 1200]
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!