# [R] Matrix scalar operation that saves memory?

Iago Giné Vázquez |@go@g|ne @end|ng |rom @jd@e@
Wed Apr 12 08:55:46 CEST 2023

```You may take a look at the bigmemory package or other which deal with large memory data in https://cran.r-project.org/web/views/HighPerformanceComputing.html#large-memory-and-out-of-memory-data
Some extra explanation is in https://stackoverflow.com/a/11127229/997979

Iago

________________________________
De: R-help <r-help-bounces using r-project.org> de part de Eric Berger <ericjberger using gmail.com>
Enviat el: dimecres, 12 d’abril de 2023 8:38
Per a: Bert Gunter <bgunter.4567 using gmail.com>
A/c: R-help <r-help using r-project.org>
Tema: Re: [R] Matrix scalar operation that saves memory?

One possibility might be to use Rcpp.
An R matrix is stored in contiguous memory, which can be considered as a
vector.
Define a C++ function which operates on a vector in place, as in the
following:

library(Rcpp)
cppFunction(
'void subtractConst(NumericVector x, double c) {
for ( int i = 0; i < x.size(); ++i)
x[i] = x[i] - c;
}')

Try this function out on a matrix. Here we define a 5x2 matrix

m <- matrix(150.5 + 1:10, nrow=5)
print(m)

[,1]  [,2]
[1,] 151.5 156.5
[2,] 152.5 157.5
[3,] 153.5 158.5
[4,] 154.5 159.5
[5,] 155.5 160.5

Now call the C++ function

subtractConst(m,100.0)
print(m)

[,1] [,2]
[1,] 51.5 56.5
[2,] 52.5 57.5
[3,] 53.5 58.5
[4,] 54.5 59.5
[5,] 55.5 60.5

HTH,
Eric

On Wed, Apr 12, 2023 at 7:34 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:

> I doubt that R's basic matrix capabilities can handle this, but have a look
> at the Matrix package, especially if your matrix is some special form.
>
> Bert
>
> On Tue, Apr 11, 2023, 19:21 Shunran Zhang <
> szhang using ngs.gen-info.osaka-u.ac.jp>
> wrote:
>
> > Hi all,
> >
> > I am currently working with a quite large matrix that takes 300G of
> > memory. My computer only has 512G of memory. I would need to do a few
> > arithmetic on it with a scalar value. My current code looks like this:
> >
> > mat <- 100 - mat
> >
> > However such code quickly uses up all of the remaining memory and got
> > the R script killed by OOM killer.
> >
> > Are there any more memory-efficient way of doing such operation?
> >
> > Thanks,
> >
> > S. Zhang
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

```