error using randi: size inputs must be scalar
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I apologize for the incredibly basic question I'm assuming it has something to do with Matlab versions but this old code that is very simple I cannot seem to make it work.
for n = 1:4
a{n} = randint(n,n,[0,9]);
b{n} = randint(n,n,[0,9]);
fprintf(’n = %d:\n’, n), A = a{n}, B = b{n},
end
I changed randint to randi per recommendation of matlab.
EDIT:
I was able to make it work by manipulating the code to this.
for n=1:4
a{n}= randi(9,n)
b{n}= randi(9,n)
fprintf('n = %d:\n', n), A = a{n}, B = b{n},
end
now generates two 4x4 matrices with random numbers between [0,9].
Can somebody explain to me why though? The first formatting looks better than the second one logically.
0 commentaires
Réponses (3)
Joseph Cheng
le 24 Avr 2017
you actually want to do
for n=1:4
a{n}= randi(10,n)-1;
b{n}= randi(10,n)-1;
fprintf('n = %d:\n', n), A = a{n}, B = b{n},
end
as randi(9) is "Generate integer values from the uniform distribution on the set 1:9."
and what you have should be correct and does more than just 2 4x4 matrices. if you scroll up in the command window (as every calculation is displayed because there is no ';' at the end of the lines)
you should see that a and b is a cell array with varying size matrices in each cell
>> a
a =
[5] [2x2 double] [3x3 double] [4x4 double]
>> b
b =
[2] [2x2 double] [3x3 double] [4x4 double]
0 commentaires
Voir également
Catégories
En savoir plus sur Simulink Functions 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!