How to store n iterations of randomly generated matrices

1 view (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 (1)

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

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!