问题描述
我必须在随机矩阵中选择一个随机列,但有时程序会选择一个大于矩阵大小的随机数。
n = 3 + (10-3)*rand(1,1);
t = round(n);
x = randi(t,t);
u = 3 + (10-3)*rand(1,1);
f = round(u);
if f < n & f ~= 0
for k = 1:t
e(k,1) = x(k,f);
end
end
所以我使用了 if 来解决这个问题,但是当命题 f < n & f ~= 0
为假时,我不知道如何重新计算随机数。请帮忙
解决方法
这不是 100% 你想要做的,但是:
t = randi( [3,10],1 ); % select a random number 't' in the range 3:10
x = randi( t,t ); % create a t-by-t matrix of random values in the range 1:t
r = randi( t ); % select a random number in the range 1:t
c = x(:,r) % select the rth column from 'x'