[R] IFELSE across large array?

Sander Oom sander at oomvanlieshout.net
Tue Nov 23 17:23:36 CET 2004


Dear all,

As our previous email did not get any response, we try again with a 
reformulated question!

We are trying to do something which needs an efficient loop over a huge 
array, possibly functions such as apply and related (tapply,
lapply...?), but can't really understand syntax and examples in 
practice...i.e. cant' make it work.

to be more specific:
we are trying to apply a mask to a 3D array.
By this I mean that when "overlaying" [i.e. comparing element by element] 
the mask on to the array the mask should change array elements according to 
the values of both array and mask elements

the mask has two values: 1 and 10.

the array elements have 3 values: 0, 1,  or 10

sticking for the moment to the single 2d array case

for example:
[A= array]  10    0 10 1  10  0
                  1   10   1 0   0 10

[ M=mask]     1  10  10 1   1  1
                 10    1   1  1 10 10

I would like the array elements to:

a) IF A(ij) !=10 and  Mij = 1
              leave A(ij) unchanged

b)  IF   A(ij) != 10 and M(ij) =10
                change A(ij) to M(ij) i.e mask value (10)

c)IF A(ij) = 10 and M(ij) = 10
               leave (Aij) unchanged

d) IF A(ij) = 10 and M(ij) !=10
            replace A(ij) with the majority value in the 8-neighborhood

(or whatever if it is an edge element) BUT ignoring 10s in this 
neighborhood (i.e. with either 1 or 0, whichever is in majority)

because the array is 3d I would like to repeat the thing with all the k 
elements (2d sub-arrays) of the array in question, using the same mask for 
al k elements

Would you be able to suggest a strategy to do this?

thanks very much

Alessandro and Sander.




More information about the R-help mailing list