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

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 5 Mai 2016

0 Stimmen

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

1 Kommentar

Swaami Jan
Swaami Jan am 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.

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 5 Mai 2016
Bearbeitet: Jan am 6 Mai 2016

0 Stimmen

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 Kommentare

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 am 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]

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 5 Mai 2016

Community Treasure Hunt

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

Start Hunting!

Translated by