問題:比較陣列以標量
欲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