From simon.urbanek at r-project.org  Wed Nov  2 02:14:54 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 1 Nov 2011 21:14:54 -0400
Subject: [Rd] reducing a too-large matrix obtained with allocMatrix()
In-Reply-To: <CAHiA-Znafaz78EG6gFo9+0ohbXsDpjQ+pyfPhmwC3RZ8KU9ZCQ@mail.gmail.com>
References: <CAHiA-Znafaz78EG6gFo9+0ohbXsDpjQ+pyfPhmwC3RZ8KU9ZCQ@mail.gmail.com>
Message-ID: <DFAE0F8A-F0A8-445D-9223-18CAC23C1875@r-project.org>


On Nov 1, 2011, at 4:29 PM, Steven Dirkse wrote:

> Hello,
> 
> I have some C code (for a shared lib called via .External)  that uses
> 
> PROTECT(w= allocMatrix(REALSXP, m, n));
> 
> mostly successfully.  In rare cases, though, the row count m will be
> an overestimate.  Is there a way to reallocate the matrix in-place,
> something like
> 
> reAllocMatrix (w,m-excess,n)    /* where excess is > 0 */
> 
> to chop off the last excess rows of w?  I only find out about the
> excess rows during the single pass over the data made to copy it from
> its source to w.
> 
> Short of this, all I know to do is allocate a matrix of the proper
> size and copy the values.  Any other ways to do this?
> 

No.

In theory you could use SETLENGTH, and then adjust the dimensions, but since you are talking about rows you'll need to re-arrange the whole matrix anyway so you would a) waste  memory in the result and b) still need to essentially copy in place in an additional pass.

Cheers,
Simon



[Rd] reducing a too-large matrix obtained with allocMatrix()
Simon Urbanek