0

我想在 -1 和 1 之间随机选择 1 x 6 个矩阵元素。

例如,[-1 1 -1 1 1 1]

怎么选??

4

2 回答 2

2

创建一个可能值的向量,并用于randi生成该向量的随机索引。

Choices = [-1 1];
Index = randi(length(Choices), 1, 6);
Choices(Index)
ans =

     1     1     1    -1     1    -1
于 2012-10-27T19:26:37.953 回答
0

Just for the sake of completeness:

randVec = 2*randi([0 1], 1,6) - 1;
于 2012-10-28T05:48:18.860 回答