MATLAB产生随机数,范围1-10, 9个数总和为60
追问:程序是什么呢?
追问:程序是什么呢?
答案:程序
sumR = 0;
while(sumR ~= 60)
R = randi(10,1,9);
sumR = sum(R);
end
结果
>> R
R =
9 6 6 10 3 8 8 4 6
>> sum(R)
ans =
60
注意,这是在matlab2009a上面运行的,matlab7.0中没有randi这个函数,可以考虑用round(10*rand(1,9)) 代替 randi(10,1,9)
如
sumR = 0;
while(sumR ~= 60)
R = round(10*rand(1,9));
sumR = sum(R);
end
>> R
R =
8 4 10 10 9 4 5 2 8
不过这个可能产生0元素,,这个就有点不好了,,
其他:这个太简单了