How to save matrix created in loop
이전 댓글 표시
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
답변 (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
Swaami Jan
2016년 5월 6일
Jan
2016년 5월 6일
This was a typo. See EDITED.
Karanvir singh Sohal
2021년 3월 1일
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]
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!