Re: [R] Creating different matrices in a loop

From: Patrick Burns <pburns_at_pburns.seanet.com>
Date: Sun, 24 Jun 2007 09:21:39 +0100

Except the list should be initialized to the final length:

smat <- vector("list", length(counts))

Growing objects is a major source of the inefficient use of memory. If the memory grows enough it becomes a large impact on execution time.

Patrick Burns
patrick_at_burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

jim holtman wrote:

>You can use a 'list' for this:
>
>counts=c(4,6,10);
>
>p=1;
>smat <- list()
>for (i in 1:length(counts))
>{
>
> smat[[i]] <- bmat[p:p+i-1,];
> p=p+i;
>}
>
>
>
>
>On 6/23/07, suman Duvvuru <duvvuru.suman_at_gmail.com> wrote:
>
>
>>Hello,
>>
>>I have a big matrix of size (20,5) -bmat . I have to loop though the rows
>>in
>>the matrix and create DIFFERENT matrices each time I go through the loop.
>>
>>counts=c(4,6,10);
>>
>>p=1;
>>for (i in 1:length(counts))
>>{
>>
>> smat=bmat[p:p+i-1,];
>> p=p+i;
>>}
>>
>>The problem is smat overwrites itself each time inside the loop. I would
>>like to have smat1, smat2, smat3 instead of a single vector smat.
>>Basically
>>I wanted to change the name of the matrix "smat" each time the loop runs
>>so
>>that i will have 3 different matrices.
>>
>>Any help will be very much appreciated.
>>
>>Thanks,
>>Suman
>>
>> [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help_at_stat.math.ethz.ch 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_stat.math.ethz.ch 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. Received on Sun 24 Jun 2007 - 08:30:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 24 Jun 2007 - 08:32:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.