Skip to content

min and max operations on matrix

2 messages · Murali.MENON at fortisinvestments.com, Gabor Grothendieck

#
Folks,

I've got a matrix x as follows:
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    5    3    4
[3,]    3    2    1


In each row of x, I want to replace the minimum value by -1, the maximum
value by +1 and all other values by 0.

So in the above case I want to end up as follows:

     [,1] [,2] [,3]
[1,]   -1    0    1
[2,]    1   -1    0
[3,]    1    0   -1

I tried the following, which seems to work:
-1; z[which.max(y)]<- 1; z})) 

Is there a neater way to do this?

Thanks,

Murali
#
Try this:
[,1] [,2] [,3]
[1,]   -1    0    1
[2,]    1   -1    0
[3,]    1    0   -1

You can avoid the transpose using plyr:
Var1  1  2  3
   1 -1  0  1
   2  1 -1  0
   3  1  0 -1
On Wed, Jan 20, 2010 at 11:57 AM, <Murali.MENON at fortisinvestments.com> wrote: