# Re: [S] Inverse function for x[col(x)<row(x)] ?

Nicole DePriest Demers (ntd@stat.ohio-state.edu)
Wed, 21 Oct 1998 23:54:16 -0400 (EDT)

On Wed, 21 Oct 1998, Randi Chen wrote:

> Dear S+ Users,
>
> I have a matrix say, x (5x5).
>
> x
> 1 6 11 16 21
> 2 7 12 17 22
> 3 8 13 18 23
> 4 9 14 19 24
> 5 10 15 20 25
>
> Then
> x.list<-x[col(x)<row(x)]
>
> will give me a list of all the elements of the
> low triangular off diagnal matrix of x
>
> x.list
>
> 2 3 4 5 8 9 10 14 15 20
>
> After some manipulation of x.list, I want to put these elements back to
> their original position, low triangular, in x.
>
> So I did the following. It works, but tedious I feel.
> "The old programming idea: looping"
>
> Do you think S+ has an inverse-function for "x[col(x)<row(x)]"?
>
>
> The following is my code to do this.
>
> If S+ does have a simpler function to do this, please let me know.
>
> A lot of thanks in advance!
>
>
> x.list
> [1] 2 3 4 5 8 9 10 14 15 20
> > cumlow<-NULL
> > for (j in 1:(5-1))
> + {
> + r0<-c(rep(0,j),x.list[1:(5-j)])
> + x.list<-x.list[(5-j+1):length(x.list)]
> + cumlow<-rbind(cumlow,r0)
> + w<-matrix(rbind(cumlow,rep(0,5)),ncol=5,byrow=T)
> + }
> >
> >
> > w
> [,1] [,2] [,3] [,4] [,5]
> [1,] 0 0 0 0 0
> [2,] 2 0 0 0 0
> [3,] 3 8 0 0 0
> [4,] 4 9 14 0 0
> [5,] 5 10 15 20 0
>
> Randi Chen
>

x_matrix(1:25,5,5)
x.list_x[col(x)<row(x)]
x.place_match(x.list,x)
w_matrix(0,5,5)
w[x.place]_x.list

> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
> w
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 2 0 0 0 0
[3,] 3 8 0 0 0
[4,] 4 9 14 0 0
[5,] 5 10 15 20 0

Nicole Demers
______________________________________________________________________________
Nicole Demers
Statistics Department
The Ohio State University
1958 Neil Ave., #404
Columbus, OH 43210, USA
ntd@stat.ohio-state.edu

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news