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

19 views (last 30 days)
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.

Answers (3)

KSSV
KSSV on 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.

Thorsten
Thorsten on 27 Oct 2016
You don't need the loop:
A = rand(3, 5, N);
  3 Comments
Jan
Jan on 4 Nov 2016
Edited: Jan on 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
MANISH KUMAR on 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.

Sign in to comment.


Stefano Gianoli
Stefano Gianoli on 26 Oct 2016
Edited: Stefano Gianoli on 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 Comments
KSSV
KSSV on 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.
Stephen23
Stephen23 on 26 Oct 2016
Edited: Stephen23 on 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).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!