问题:比较阵列以标量
欲lessthanorequalto操作比较的二维阵列和一个标量变量,都与数值与(< =)和阵列中分配所有这些值到一个矢量。
我想在R加速此任务。
现在,下面是我使用的代码(这显然是相当费时)
代码我现在使用:
2d_examplearray; #我的2维数组实际大小3500 X 4200 my_scalarvariable = 5; #一些任意值,因为这是一个例子
dims_2darray = dim(2d_examplearray); #不要。行&列的信息
# First create and then initialize vectors for storing values accordingly as specified in if # condition below
eachelementin_ltvector<-vector();
eachelementin_gtvector<-vector();
eachelementin_ltvector=1;
eachelementin_gtvector=1;
for (eachrow in 1 : dims_2darray[1])
{
for (eachcol in 1 : dims_2darray[2])
{
if(2d_examplearray[eachrow,eachcol]<my_scalarvariable)
{
vector_lessthanvalue[eachelementin_ltvector]=2d_examplearray[eachrow,eachcol];
eachelementin_ltvector=eachelementin_ltvector+1;
}
else # greater than or equal to my scalar variable then
{
vector_greaterthanvalue[eachelementin_gtvector]=2d_examplearray[eachrow,eachcol];
eachelementin_gtvector=eachelementin_gtvector+1;
}
}
}
感谢就同样的问题在我以前的帖子的输入。 我是新来的R和这个Q &论坛。
再次感谢
请出示基于这样一个小例子,预期的输出。有关指南,请查看[here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – akrun
您是否尝试过谷歌R子集? – nicola
'indx < - yourmatrix <= scalarvariable; yourmatrix [indx] < - vector1'但是,vector1的长度应该与'yourmatrix'的长度相同。没有一个可重复的例子,代码不容易 – akrun