s-news
[Top] [All Lists]

Re: [S] filling a sparse matrix

To: Siem Heisterkamp <s.h.heisterkamp@amc.uva.nl>
Subject: Re: [S] filling a sparse matrix
From: Douglas Bates <bates@stat.wisc.edu>
Date: 20 Jan 1999 12:18:48 -0600
Cc: "s-news (E-mail)" <S-news@wubios.wustl.edu>, "'s-nieuws'" <s-nieuws@candiensten.nl>
In-reply-to: Siem Heisterkamp's message of "Wed, 20 Jan 1999 17:09:50 +0100"
References: <87CDBA129891D111896900A0C944DD240D2545@poweredge.amc.uva.nl>
Sender: owner-s-news@wubios.wustl.edu
>>>>> "Siem" == Siem Heisterkamp <s.h.heisterkamp@amc.uva.nl> writes:

  Siem> dear S-Plus users, What do I wrong? I try to fill a sparse
  Siem> matrix. Suppose I have a (large) matrix n*k X and data resid
  Siem> unequal zero of length m.  I also have row.num and col.num
  Siem> both of length m indicating each element of which row and
  Siem> collumn corresponds with a value of resid.  I thought to be
  Siem> clever, using the assignment :

  Siem> X[row.num, col.num]<-resid[1:m]


  Siem> It does not seem to work!

  Siem> Anybody a bright idea?

Form row.num and col.num into a matrix with m rows and 2 columns using
cbind() then use the matrix for a subscript.  This uses an obscure
capability of S-PLUS indexing.

S> ttt <- matrix(0.0, nrow = 30, ncol = 5)
S> row.num <- sample(1:nrow(ttt), 12, replace = TRUE)
S> col.num <- sample(1:ncol(ttt), 12, replace = TRUE)
S> vals <- rnorm(12)
S> cbind( row.num, col.num, vals )
      [,1] [,2]      [,3]
 [1,]   23    3 -0.490807
 [2,]   23    1  0.410475
 [3,]   25    2  0.878596
 [4,]   10    3 -0.077786
 [5,]   12    2  1.294594
 [6,]   12    5 -0.701333
 [7,]   17    3 -0.886124
 [8,]   13    5  1.453320
 [9,]   25    5 -0.583600
[10,]   23    3 -1.363839
[11,]   13    3 -0.276606
[12,]   25    2 -1.378772
S> ttt[ cbind( row.num, col.num) ] <- vals
S> ttt
         [,1]    [,2]      [,3] [,4]     [,5]
 [1,] 0.00000  0.0000  0.000000    0  0.00000
 [2,] 0.00000  0.0000  0.000000    0  0.00000
 [3,] 0.00000  0.0000  0.000000    0  0.00000
 [4,] 0.00000  0.0000  0.000000    0  0.00000
 [5,] 0.00000  0.0000  0.000000    0  0.00000
 [6,] 0.00000  0.0000  0.000000    0  0.00000
 [7,] 0.00000  0.0000  0.000000    0  0.00000
 [8,] 0.00000  0.0000  0.000000    0  0.00000
 [9,] 0.00000  0.0000  0.000000    0  0.00000
[10,] 0.00000  0.0000 -0.077786    0  0.00000
[11,] 0.00000  0.0000  0.000000    0  0.00000
[12,] 0.00000  1.2946  0.000000    0 -0.70133
[13,] 0.00000  0.0000 -0.276606    0  1.45332
[14,] 0.00000  0.0000  0.000000    0  0.00000
[15,] 0.00000  0.0000  0.000000    0  0.00000
[16,] 0.00000  0.0000  0.000000    0  0.00000
[17,] 0.00000  0.0000 -0.886124    0  0.00000
[18,] 0.00000  0.0000  0.000000    0  0.00000
[19,] 0.00000  0.0000  0.000000    0  0.00000
[20,] 0.00000  0.0000  0.000000    0  0.00000
[21,] 0.00000  0.0000  0.000000    0  0.00000
[22,] 0.00000  0.0000  0.000000    0  0.00000
[23,] 0.41047  0.0000 -1.363839    0  0.00000
[24,] 0.00000  0.0000  0.000000    0  0.00000
[25,] 0.00000 -1.3788  0.000000    0 -0.58360
[26,] 0.00000  0.0000  0.000000    0  0.00000
[27,] 0.00000  0.0000  0.000000    0  0.00000
[28,] 0.00000  0.0000  0.000000    0  0.00000
[29,] 0.00000  0.0000  0.000000    0  0.00000
[30,] 0.00000  0.0000  0.000000    0  0.00000
-----------------------------------------------------------------------
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

<Prev in Thread] Current Thread [Next in Thread>