# how to run a certain code loop for 'N' times and get 'N' number of output outside the loop.

조회 수: 71(최근 30일)
MANISH KUMAR 26 Oct 2016
편집: Stefano Gianoli 26 Jul 2017
I have certain code which I run for 'N' times using 'for' loop but after running the loop when I want to get all the N value of the parameters, it only gives the Last (Nth) value. for example this code generates the five matrices if I put A inside.
clc clear all N = 5; for i = 1:N A = rand(3,5); A end
% output is
A =
0.4352 0.9375 0.5505 0.2475 0.3547
0.1577 0.1078 0.4274 0.4474 0.7731
0.6005 0.9000 0.1524 0.5328 0.8817
A =
0.7341 0.6411 0.3105 0.4269 0.9250
0.4064 0.1275 0.5786 0.0331 0.3583
0.6042 0.4962 0.9436 0.9294 0.2600
A =
0.7869 0.6848 0.9429 0.9094 0.6503
0.5116 0.0924 0.0966 0.0113 0.3851
0.5625 0.8726 0.8459 0.5237 0.6493
A =
0.7629 0.2782 0.6316 0.4008 0.0904
0.5757 0.8398 0.8335 0.5543 0.7444
0.6319 0.4268 0.2702 0.4439 0.0326
A =
0.4297 0.5223 0.8845 0.8946 0.5676
0.0373 0.9096 0.2550 0.3985 0.8945
0.9758 0.3832 0.9090 0.6250 0.2142
% If I put 'A' outside the loop it gives only the last (5th) value.
clc clear all N = 5; for i = 1:N A = rand(3,5); end A
A =
0.4297 0.5223 0.8845 0.8946 0.5676
0.0373 0.9096 0.2550 0.3985 0.8945
0.9758 0.3832 0.9090 0.6250 0.2142
I want all the five matrices outside the loop as A1, A2, A3,A4 and A5. Please help me.

로그인 to comment.

### 답변(3개)

KSSV 26 Oct 2016
Make A a 3D matrix.
N = 5;
A = zeros(3,5,N) ;
for i = 1:N
A(:,:,i) = rand(3,5);
end
You can access A using A(:,:,i) where i = 1,2,3,4,5.

#### 댓글 수: 0

로그인 to comment.

Thorsten 27 Oct 2016
You don't need the loop:
A = rand(3, 5, N);

#### 댓글 수: 3

MANISH KUMAR 4 Nov 2016
Sir, actually the Matrices I need have some constraints. I give you the code than suggest me how to get 'N' matrices using this code.
the code is
clc
clear all
N = 5; % Population size
P = 5; % The number of competitive projects
T = 10; % The number of time periods
% The Expected benefit in each time period
B = [25 23 20 17 16 14 10 0 0 0; 30 28 25 21 19 17 0 0 0 0; 20 19 17 15 14 12 10 9 0 0; 15 13 12 11 11 10 7 5 3 0; 25 24 21 19 17 14 8 0 0 0];
% Duration of projects
D = [4; 5; 3; 2; 4];
% Resource requirement of a project in each period
% Res Req (type 1) = [3; 5; 1; 2; 4]; Max Availability = 6
R1 = [3*ones(1,10);5*ones(1,10);ones(1,10);2*ones(1,10);4*ones(1,10)];
% Res Req (type 2) = [2; 4; 1; 2; 3]; Max Availability = 4
R2 = [2*ones(1,10);4*ones(1,10);ones(1,10);2*ones(1,10);3*ones(1,10)];
excludedcount = D-1;
X = zeros(P,T);
for i = 1:N
while(1)
for row = 1:P
rv = [1, zeros(1, T - excludedcount(row) - 1)];
X(row, 1 : (T - excludedcount(row))) = rv(randperm(numel(rv)));
end
X;
if any(X(1,:))
X([4 5],:)=0;
end
if any(X(2,:))
X(4,:)=0;
end
if any(X(3,:))
X(5,:)=0;
end
if any(X(4,:))
X(2,:)=0;
end
if any(X(5,:))
X([1 3],:)=0;
end
X;
Q = X.*B;
TF = sum(sum(Q));
Y=X;
for row = 1:P
ic=find(Y(row,:));
if ~isempty(ic)
Y(row,ic:ic+D(row)-1)=ones(1,D(row));
end
end
Y;
RR1 = R1.*Y;
CRR1 = sum(RR1);
RR2 = R2.*Y;
CRR2 = sum(RR2);
if ((max(CRR1)>6)||(max(CRR2)>4))
continue;
else
break;
end
CRR1;
CRR2;
end
X
TF
Y
RR1
RR2
CRR1
CRR2
end
Sir i want this matrix X which gives N=5 different matrices inside the loop but outside the loop it gives the last on only.
Jan 4 Nov 2016
@MANISH KUMAR: Please use the "{} Code" button to post readable code. Do not hide an important detail of the question in the comment of an answer, but all required details shsould be found inside the original question. Therefore use the possibility to edit the question. Thanks.
You got several suggestion already. All you have to do is storing the matrix in each iteration.
Result = cell(1, N);
for i = 1:N
...
ResultC{i} = X;
end
...
% Perhaps:
ResultM = cat(3, ResultC{:});
MANISH KUMAR 10 Nov 2016
@Jan Simon: Sir thanks for the kind support and sorry for the mistake. I will keep this in mind next time. Thank you very much again.

로그인 to comment.

Stefano Gianoli 26 Oct 2016
편집: Stefano Gianoli 26 Jul 2017
You might allocate the memory required for A so it can hold 3 x 5 x N elements instead of just 3 x 5:
>>N = 5; for i = 1:N A(:,:,i) = rand(3,5); end, A
A(:,:,1) =
0.8147 0.9134 0.2785 0.9649 0.9572
0.9058 0.6324 0.5469 0.1576 0.4854
0.1270 0.0975 0.9575 0.9706 0.8003
A(:,:,2) =
0.1419 0.7922 0.0357 0.6787 0.3922
0.4218 0.9595 0.8491 0.7577 0.6555
0.9157 0.6557 0.9340 0.7431 0.1712
A(:,:,3) =
0.7060 0.0462 0.6948 0.0344 0.7655
0.0318 0.0971 0.3171 0.4387 0.7952
0.2769 0.8235 0.9502 0.3816 0.1869
A(:,:,4) =
0.4898 0.7094 0.6797 0.1190 0.3404
0.4456 0.7547 0.6551 0.4984 0.5853
0.6463 0.2760 0.1626 0.9597 0.2238
A(:,:,5) =
0.7513 0.6991 0.5472 0.2575 0.8143
0.2551 0.8909 0.1386 0.8407 0.2435
0.5060 0.9593 0.1493 0.2543 0.9293
you might also preallocate A for speed:
>> A = zeros(3,5,N);

#### 댓글 수: 2

KSSV 26 Oct 2016
N = 5; for i = 1:N, eval(sprintf('A%i = rand(3,5);A%i',i,i)), end
This is not suggested.
Stephen Cobeldick 26 Oct 2016
@Stefano Gianoli: is there a reason why you are advising other beginners to use buggy and slow eval for such a trivial bit of code? eval is totally unnecessary in this code, and is best avoided (especially by beginners).

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by