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개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 5월 5일

0 개 추천

B{i}=a(randperm(k),:);

댓글 수: 1

Swaami Jan
Swaami Jan 2016년 5월 5일
Thanks for your reply. But I want the final output in one matrix. That means 2nd output from B will be saved below the first output.
and my "i" is not continuous. It takes values 1, 6, 11, 16. .... etc.

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

Jan
Jan 2016년 5월 5일
편집: Jan 2016년 5월 6일

0 개 추천

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

Thanks Simon for the code. Your code is giving me below error
" Attempted to access index(0); index must be a positive integer or logical." i = index(c);
for c = i:len
i = index(c);
a = A(i:i+(k-1), :);
B{c} = a(randperm(k), :);
end
I tried modifying the code and added one more condition. See below
for c=i:len
if (i>0)
i = index(c);
a= A(i+i(k-1),:);
B{c}=a(randperm(k),:);
end
end
But its is giving " Warning: Colon operands must be real scalars." for the below line
for c=i:len
Jan
Jan 2016년 5월 6일
This was a typo. See EDITED.
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에 대해 자세히 알아보기

질문:

2016년 5월 5일

댓글:

2021년 3월 1일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by