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

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 5 Mai 2016
B{i}=a(randperm(k),:);

1 commentaire

Swaami Jan
Swaami Jan le 5 Mai 2016
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.

Connectez-vous pour commenter.

Jan
Jan le 5 Mai 2016
Modifié(e) : Jan le 6 Mai 2016
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

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 le 6 Mai 2016
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]

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by