R 中的 Matlab find() 函数
Matlab find() function in R
我正在尝试将一些 Matlab 代码转换为 R。代码如下所示:
u= [.4 .5 .1;
.2 .4 .4;
.4 .1 .5]
z= find(cumsum(u)>= rand,1)
OUTPUT: e.g. z = 3
数学背景是这样描述的(来自数学教程脚本):
We make a vector containing the cumulative sum of the columns (which
we know sum to one), generate a random number (0-1),and then use the
find function to take the first number in the cumulative sum vector
that is >= the random number. For example if our D vector is [.5 .5]
50% of the time the element of the vector corresponding to the state
one will be >= to the random number. https://psyarxiv.com/b4jm6/
我尝试使用 pracma
包中的 find()
(下面的文档。link),但我不知道如何让它工作。我得到 "dims[product...] do not match the length of object"
或大部分只是 "is.character(what) is not TRUE"
...
Matlab函数:
k = find(x,n)
returns 对应于 X 中非零元素的前 n 个索引。
https://de.mathworks.com/help/matlab/ref/find.html
find()
来自 R 包 pracma
,似乎只转换 find(x)
:
https://www.rdocumentation.org/packages/pracma/versions/1.1.0/topics/find
由@Rui Barradas 解决:which(cumsum(u)>= runif(1))[1]
我正在尝试将一些 Matlab 代码转换为 R。代码如下所示:
u= [.4 .5 .1;
.2 .4 .4;
.4 .1 .5]
z= find(cumsum(u)>= rand,1)
OUTPUT: e.g. z = 3
数学背景是这样描述的(来自数学教程脚本):
We make a vector containing the cumulative sum of the columns (which we know sum to one), generate a random number (0-1),and then use the find function to take the first number in the cumulative sum vector that is >= the random number. For example if our D vector is [.5 .5] 50% of the time the element of the vector corresponding to the state one will be >= to the random number. https://psyarxiv.com/b4jm6/
我尝试使用 pracma
包中的 find()
(下面的文档。link),但我不知道如何让它工作。我得到 "dims[product...] do not match the length of object"
或大部分只是 "is.character(what) is not TRUE"
...
Matlab函数:
k = find(x,n)
returns 对应于 X 中非零元素的前 n 个索引。 https://de.mathworks.com/help/matlab/ref/find.html
find()
来自 R 包 pracma
,似乎只转换 find(x)
:
https://www.rdocumentation.org/packages/pracma/versions/1.1.0/topics/find
由@Rui Barradas 解决:which(cumsum(u)>= runif(1))[1]