[R] Filling matrices

Bert Gunter gunter.berton at gene.com
Mon Mar 19 22:24:31 CET 2012


Please folks!

Why issue protestations of  "clumsiness" when all you have to do is
make some effort to learn R?

See section 5.3 of an Intro to R -- which ships with every copy of R
-- on matrix indexing.

?"["

also documents the behavior, albeit more tersely. ("... a third form
of indexig ...")

(ergo the process is: Create a matrix with 0's; use matrix indexing to
populate the rows and columns of choice).

Of course, the best solution may be just to use the SparseM package to
handle it. But as I have not used it (except indirectly) ...

-- Bert

On Mon, Mar 19, 2012 at 2:16 PM, Peter Langfelder
<peter.langfelder at gmail.com> wrote:
> On Mon, Mar 19, 2012 at 2:07 PM, David Stevens <david.stevens at usu.edu> wrote:
>> I'm a bit clumsy about many things in R. Here's my problem. I'm trying to
>> build a square sparse matrix and populate it without looping (bad practice,
>> right). I have vectors of matched row/column pairs for which the matrix
>> entries have common characteristics and am look for a way to fill the
>> entries. So, if the matrix is A[20 by 20], and I might have rows
>>
>>  iRows <- c(2,3,4,6,7,8,10,11,12,14,15,16,18,19)
>>
>> and columns
>>
>>  iCols <- c(1,2,3,5,6,7,9,10,11,13,14,15,17,18)
>>
>> and you see these are most of the subdiagonal terms in A from rows 2-19.
>> They are all calculated in a similar way using values from a data frame in
>> which the terms are generally in iRows and iCols.
>>
>> I could loop through each pair and all's well, but my question is whether
>> there's an R-certified alternative, that will speed things up when the
>> matrix is much larger (it will be - this is a prototype).
>>
>> Any thoughts?
>
> This isn't very elegant, but it's a solution, and probably quite fast
> on large matrices:
>
> n = 20 # matrix dimension
> # Calculate the indices of the elements when the matrix is turned into
> a single linear vector
> indices = (iCols-1) * n + iRows
> # Fill the vector
> A.vector = rep(0, n^2);
> A.vector[indices] = values;
> # Pick one: Assign the values from the vector to the matrix
> A[, ] = A.vector
> # Or re-dimension the vector to a matrix
> # if copying is to be avoided)
> dim(A.vector) = c(n,n); A=A.vector;
>
> HTH,
>
> Peter
>>
>> David S
>>
>> --
>> David K Stevens, P.E., Ph.D., Professor
>> Civil and Environmental Engineering
>> Utah Water Research Laboratory
>> 8200 Old Main Hill
>> Logan, UT  84322-8200
>> 435 797 3229 - voice
>> 435 797 1363 - fax
>> david.stevens at usu.edu
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list