How to store n iterations of randomly generated matrices

2 vues (au cours des 30 derniers jours)
N/A
N/A le 29 Juil 2022
Réponse apportée : Torsten le 29 Juil 2022
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

Réponses (1)

Torsten
Torsten le 29 Juil 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

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by