# Re: [R] Analog to matlab repmat function

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon 29 May 2006 - 16:27:22 EST

On Mon, 29 May 2006, Daniil Ivanov wrote:

> Oops, mistake, should be
>
> data = data - repmat(mean(data,1),[130 1 1 1]);
>
> Sorry.

I am afraid I do not know what that actually does in matlab, except that it looks like subtracting (not extracting) a mean.

Functions which I would expect you to be using are sweep(), rowMeans or colMeans(). Something like

arr <- sweep(arr, 2:4, colMeans(arr, 1))

perhaps? (Using 'data', the name of a system R object, is a good way to sidetrack your audience and so best avoided.)

> On 5/29/06, Daniil Ivanov <daniil.ivanov@gmail.com> wrote:
>> Hello,
>>
>> I'm trying to switch from Matlab to R-project, and having some difficulties.
>> I make a use of multidimensional matrices. For example, I need to extract
>> mean from one of the dimensions:
>>
>> % we have matrix data of size: 130 x 11 x 350 x 2
>> data = data - repmat(mean(data,3),[130 1 1 1]);
>>
>> In R project I managed to do that in a very pervarsive way:
>>
>> # mean(data,3) in R
>> base <- apply(data,c(2,3,4),mean)

Probably rowMeans(data, 1) is meant.

>> # repmat(...,[130 1 1 1])
>> base <- rep(base,130)
>> dim(base) <- c(11,2,350,130)
>> base <- aperm(base,c(4,1,2,3))
>> # data = data - repmat
>> data <- data - base;
>>
>> Could you please show me a more elegant way of doing the same in R :)
>>
>> Thanks, Daniil.
>>
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>

```--
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help