从另一个数组的条件构建数组

时间:2021-11-21 21:33:10

I am new to matlab. I want to do the following:

我是matlab新手。我想做的是:

  • Generate an array of a thousand replications of a random draws between three alternatives A,B and C, where at every draw, each alternative has the same probability to be picked.
  • 在三个选项a、B和C之间生成一个一千次随机抽取的数组,在每个抽取中,每个选项都有相同的被抽取的概率。

So eventually I need something like P = [ A A B C B B B C A C A C C ... ] where each element in the array was chosen randomly among the three possible outcomes.

所以最终我需要P = [A B B B B C C C C C C C C C C。[参考译文]数组中的每个元素都是在三个可能的结果中随机选择的。

I came up with a solution which gives me exactly what I want, namely

我想出了一个解决方案,它正好给了我想要的,也就是

% Generating random pick among doors 1,2,3, where 1 stands for A, 2 for B,
% 3 for B.

I = rand(1);

if I < 1/3
    PP = 1;
elseif 1/3 <= I & I < 2/3
    PP = 2;
else 
    PP = 3;
end 

% Generating a thousand random picks among dors A,B,C

I = rand(999);

for i=1:999    
if I(i) < 1/3
    P = 1;
elseif 1/3 <= I(i) & I(i) < 2/3
    P = 2;
else 
    P = 3;
end 
PP = [PP  P]
end

As I said, it works, but when I run the procedure, it takes a while for what appears to me as a simple task. At the same time, I long such a task is "supposed" to take in matlab. So I have three question:

正如我所说的,它是有效的,但是当我运行这个过程时,它需要一段时间来完成在我看来很简单的任务。与此同时,我很长一段时间以来,这样的任务就是“应该”去学习matlab。所以我有三个问题:

  1. Is this really a slow procedure to generate the desired outcome?
  2. 这真的是一个产生期望结果的缓慢过程吗?
  3. If it is, why is this procedure particularly slow?
  4. 如果是的话,为什么这个过程特别慢?
  5. What would be a more effective way to produce the desired outcome?
  6. 有什么更有效的方法可以产生预期的结果?

2 个解决方案

#1


4  

This can be done much easier with randi

对于randi,这可以做得更简单。

>> PP = randi(3,1,10)
PP =
     2     1     3     3     2     2     2     3     2     1

If you actually want to choose between 3 alternatives, you use the output of randi directly to index into another matrix.

如果你真的想在3个选项中选择,你可以直接使用randi的输出来索引到另一个矩阵。

>> options = [13,22,77]
options =
    13    22    77
>> options(randi(3,1,10))
ans =
    22    13    77    13    77    13    22    22    77    13

As to the reason why your solution is slow, you do something similar to this:

至于你的解决方案慢的原因,你做了类似的事情:

x = [];
for i=1:10
    x = [x i^2]; %size of x grows on every iteration
end

This is not very good, since on every iteration, Matlab needs to allocate space for a larger vector x. In old versions of Matlab, this lead to quadratic behavior (if you double the size of the problem, it takes 4 times longer). In newer versions, Matlab is smart enough to avoid this problem. It is however still considered nice to preallocate space for your array if you know beforehand how big it will be:

这不是很好,因为在每一次迭代中,Matlab需要为一个更大的向量x分配空间。在Matlab的旧版本中,这会导致二次行为(如果你把问题的大小增加一倍,它需要花费4倍的时间)。在较新的版本中,Matlab足够聪明,可以避免这个问题。不过,如果事先知道数组的大小,那么预先为数组分配空间仍然被认为是不错的:

x = zeros(1,10); % space for x is preallocated. can also use nan() or ones()
for i = 1:length(x)
    x(i) = i^2;
end

But in many cases, it is even faster to use vectorized code that does not use any for-loops like so:

但在许多情况下,使用矢量化代码而不使用任何for循环会更快:

x = (1:10).^2;

All 3 solutions give the same result:

三个解都给出相同的结果:

x =    1     4     9    16    25    36    49    64    81   100

#2


0  

cnt=10;
option={'a','b','c'}
x=option([randi(numel(option),cnt,1)])

#1


4  

This can be done much easier with randi

对于randi,这可以做得更简单。

>> PP = randi(3,1,10)
PP =
     2     1     3     3     2     2     2     3     2     1

If you actually want to choose between 3 alternatives, you use the output of randi directly to index into another matrix.

如果你真的想在3个选项中选择,你可以直接使用randi的输出来索引到另一个矩阵。

>> options = [13,22,77]
options =
    13    22    77
>> options(randi(3,1,10))
ans =
    22    13    77    13    77    13    22    22    77    13

As to the reason why your solution is slow, you do something similar to this:

至于你的解决方案慢的原因,你做了类似的事情:

x = [];
for i=1:10
    x = [x i^2]; %size of x grows on every iteration
end

This is not very good, since on every iteration, Matlab needs to allocate space for a larger vector x. In old versions of Matlab, this lead to quadratic behavior (if you double the size of the problem, it takes 4 times longer). In newer versions, Matlab is smart enough to avoid this problem. It is however still considered nice to preallocate space for your array if you know beforehand how big it will be:

这不是很好,因为在每一次迭代中,Matlab需要为一个更大的向量x分配空间。在Matlab的旧版本中,这会导致二次行为(如果你把问题的大小增加一倍,它需要花费4倍的时间)。在较新的版本中,Matlab足够聪明,可以避免这个问题。不过,如果事先知道数组的大小,那么预先为数组分配空间仍然被认为是不错的:

x = zeros(1,10); % space for x is preallocated. can also use nan() or ones()
for i = 1:length(x)
    x(i) = i^2;
end

But in many cases, it is even faster to use vectorized code that does not use any for-loops like so:

但在许多情况下,使用矢量化代码而不使用任何for循环会更快:

x = (1:10).^2;

All 3 solutions give the same result:

三个解都给出相同的结果:

x =    1     4     9    16    25    36    49    64    81   100

#2


0  

cnt=10;
option={'a','b','c'}
x=option([randi(numel(option),cnt,1)])