Problem:
I want to compare a 2-dimensional array and a scalar variable, both with numerical values with (<=) lessthanorequalto operation and assign all those values in the array to a vector.
I want to speed up this task in R.
Now, below is the code I am using (which is obviously quite time consuming)
Code I am using now :
2d_examplearray; # My 2-dimensional array actual size 3500 X 4200 my_scalarvariable=5; # some arbitrary value as this is an example
dims_2darray=dim(2d_examplearray); # Get no. of rows & columns information
# 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;
}
}
}
Thanks for the inputs on my previous post regarding the same question. I am new to R and this Q&A forum.
Thanks again