How to store n iterations of randomly generated matrices

3 views (last 30 days)
To whom may be able to provide help,
Below is an algorithm that computes randomly generated matrices (of size 3x3). The values of the main diagonal are generated randomly, while the non-diagonal values are zero (as intentionally set). My goal is to store n randomly generated matrices in an array so that I may later plot them . How can I achieve this? The code as I have set-up gives me all zero matrices except the last iteration.
n = 10 ;
bounds = [1 100] ; %interval over which the random number generator will produce values
for i = 1:n
randA = diag(diag(randi(bounds, 3,3)))
randA_all(:,:,n) = randA
end

Answers (2)

Dyuman Joshi
Dyuman Joshi on 29 Jul 2022
Edited: Dyuman Joshi on 30 Jul 2022
n = 10 ;
bounds = [1 100] ; %interval over which the random number generator will produce values
randA_all=zeros(3,3,n);
for i = 1:n
randA_all(:,:,i) = diag(diag(randi(bounds, 3,3)));
%i instead of n
end
randA_all
randA_all =
randA_all(:,:,1) = 21 0 0 0 92 0 0 0 39 randA_all(:,:,2) = 7 0 0 0 34 0 0 0 66 randA_all(:,:,3) = 41 0 0 0 13 0 0 0 56 randA_all(:,:,4) = 86 0 0 0 6 0 0 0 86 randA_all(:,:,5) = 33 0 0 0 15 0 0 0 31 randA_all(:,:,6) = 89 0 0 0 24 0 0 0 80 randA_all(:,:,7) = 71 0 0 0 14 0 0 0 99 randA_all(:,:,8) = 73 0 0 0 5 0 0 0 68 randA_all(:,:,9) = 82 0 0 0 72 0 0 0 84 randA_all(:,:,10) = 75 0 0 0 65 0 0 0 8

Torsten
Torsten on 29 Jul 2022
n = 10 ;
bounds = [1 100] ; %interval over which the random number generator will produce values
randA = zeros(3,3);
randA_all = zeros(3,3,n);
for i = 1:n
randA = diag(diag(randi(bounds, 3,3)));
randA_all(:,:,i) = randA;
end

Community Treasure Hunt

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

Start Hunting!

Translated by