Analog to matlab repmat function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Analog to matlab repmat function

Daniil Ivanov-2
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)
 # 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.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Analog to matlab repmat function

Daniil Ivanov-2
Oops, mistake, should be

data = data - repmat(mean(data,1),[130 1 1 1]);

Sorry.

On 5/29/06, Daniil Ivanov <[hidden email]> 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)
>  # 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.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Analog to matlab repmat function

Gabor Grothendieck
Check out ?sweep

On 5/28/06, Daniil Ivanov <[hidden email]> wrote:

> Oops, mistake, should be
>
> data = data - repmat(mean(data,1),[130 1 1 1]);
>
> Sorry.
>
> On 5/29/06, Daniil Ivanov <[hidden email]> 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)
> >  # 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.
> >
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Analog to matlab repmat function

Prof Brian Ripley
In reply to this post by Daniil Ivanov-2
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 <[hidden email]> 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.
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Analog to matlab repmat function

Daniil Ivanov-2
In reply to this post by Daniil Ivanov-2
Thanks a lot to all of you!

 Now I see, that use of R (and S) is very different from use of Matlab.
 There is no one-to-one correspondence.
 I'm shy to ask so stupid questions, but name of language R
 makes it hard to find relevant links trough google.
 Thanks for pointing to "S Poetry", I would never find it alone,
 since I was looking for R-related info.

Thanks, Daniil.

On 5/29/06, Patrick Burns <[hidden email]> wrote:

> S Poetry may be of use to you.
>
>
> Patrick Burns
> [hidden email]
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
>
> Daniil Ivanov 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)
> > # 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.
> >
> >______________________________________________
> >[hidden email] mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
> >
> >
> >
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Analog to matlab repmat function

Gabor Grothendieck
Actually google does understand R.  For example,


1. google for:

   R

and you will get the R home page.as first hit

2. google for

   R repmat

and you will find Robin Hankin's lexicon that translates between
R and octave/maxima which includes a repmat function as 7th hit.

On 5/29/06, Daniil Ivanov <[hidden email]> wrote:

> Thanks a lot to all of you!
>
>  Now I see, that use of R (and S) is very different from use of Matlab.
>  There is no one-to-one correspondence.
>  I'm shy to ask so stupid questions, but name of language R
>  makes it hard to find relevant links trough google.
>  Thanks for pointing to "S Poetry", I would never find it alone,
>  since I was looking for R-related info.
>
> Thanks, Daniil.
>
> On 5/29/06, Patrick Burns <[hidden email]> wrote:
> > S Poetry may be of use to you.
> >
> >
> > Patrick Burns
> > [hidden email]
> > +44 (0)20 8525 0696
> > http://www.burns-stat.com
> > (home of S Poetry and "A Guide for the Unwilling S User")
> >
> > Daniil Ivanov 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)
> > > # 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.
> > >
> > >______________________________________________
> > >[hidden email] mailing list
> > >https://stat.ethz.ch/mailman/listinfo/r-help
> > >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> > >
> > >
> > >
> > >
> >
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

newbie question: ROW average

A Ezhil
In reply to this post by Daniil Ivanov-2
Hi,

I am new to R programming. I have a 992 x 74 matrix. I
would like to form a new matrix by averging each 4
rows
from the original one. How can I use 'apply' instead
of usual mean inside the nested for loop?

Thanks in advance.

regards,
ezhil

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Dimitris Rizopoulos
look at ?rowMeans; you can also use "apply(mat, 1, mean)" but
rowMeans() is better.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: "A Ezhil" <[hidden email]>
To: "r-help" <[hidden email]>
Sent: Monday, May 29, 2006 1:24 PM
Subject: [R] newbie question: ROW average


> Hi,
>
> I am new to R programming. I have a 992 x 74 matrix. I
> would like to form a new matrix by averging each 4
> rows
> from the original one. How can I use 'apply' instead
> of usual mean inside the nested for loop?
>
> Thanks in advance.
>
> regards,
> ezhil
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Peter Dalgaard
In reply to this post by A Ezhil
A Ezhil <[hidden email]> writes:

> Hi,
>
> I am new to R programming. I have a 992 x 74 matrix. I
> would like to form a new matrix by averging each 4
> rows
> from the original one. How can I use 'apply' instead
> of usual mean inside the nested for loop?

How about

dim(M) <- c(4,248,74)
mn <- apply(M, c(2,3), mean)
 
(Yes, this is a bit confusing and easy to get wrong...)

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Rolf Turner-2
In reply to this post by A Ezhil
Dimitris Rizopoulos wrote:

> look at ?rowMeans; you can also use "apply(mat, 1, mean)" but
> rowMeans() is better.

        By my reading of the question, this is not what
        Ezhil wants.  He said:

        ``I have a 992 x 74 matrix. I would like to form a new matrix
          by averaging each 4 rows from the original one.''

        I.e. he wants (I think) the first row of the new matrix
        to be the mean of the first 4 rows of the old one, the
        second row of the new matrix to be the mean of rows 5
        through 8 of the old one, and so on.

        One way this could be done is via

        > m.new <- t(apply(array(t(m.old),dim=c(74,4,992/4)),c(1,3),mean))

                        cheers,

                                Rolf Turner
                                [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Rolf Turner-2
Peter Dalgaard wrote:

> How about
>
> dim(M) <- c(4,248,74)
> mn <- apply(M, c(2,3), mean)

        Hey!  That's sexy!  Much better than my kludgy
        suggestion!

                        cheers,

                                Rolf

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Dimitris Rizopoulos
In reply to this post by Rolf Turner-2
yes you're right; it was my mistake.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: "Rolf Turner" <[hidden email]>
To: <[hidden email]>; <[hidden email]>
Cc: <[hidden email]>
Sent: Monday, May 29, 2006 1:55 PM
Subject: Re: [R] newbie question: ROW average


> Dimitris Rizopoulos wrote:
>
>> look at ?rowMeans; you can also use "apply(mat, 1, mean)" but
>> rowMeans() is better.
>
> By my reading of the question, this is not what
> Ezhil wants.  He said:
>
> ``I have a 992 x 74 matrix. I would like to form a new matrix
>   by averaging each 4 rows from the original one.''
>
> I.e. he wants (I think) the first row of the new matrix
> to be the mean of the first 4 rows of the old one, the
> second row of the new matrix to be the mean of rows 5
> through 8 of the old one, and so on.
>
> One way this could be done is via
>
> > m.new <- t(apply(array(t(m.old),dim=c(74,4,992/4)),c(1,3),mean))
>
> cheers,
>
> Rolf Turner
> [hidden email]
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Gabor Grothendieck
In reply to this post by A Ezhil
Try this:

   rowsum(mat, gl(nrow(mat)/4, 4)) / 4

On 5/29/06, A Ezhil <[hidden email]> wrote:

> Hi,
>
> I am new to R programming. I have a 992 x 74 matrix. I
> would like to form a new matrix by averging each 4
> rows
> from the original one. How can I use 'apply' instead
> of usual mean inside the nested for loop?
>
> Thanks in advance.
>
> regards,
> ezhil
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

A Ezhil
In reply to this post by Dimitris Rizopoulos
Hi,

Thank you all (Dimitris, Peter, Rolf, Gabor) for your
suggestions. I tried with all your suggestions. I am
getting different answers when I use:

rowsum(mat, gl(nrow(mat)/4, 4)) / 4

and

m.new <-
t(apply(array(t(m.old),dim=c(74,4,992/4)),c(1,3),mean))

When I tried with (assuming 'M' is my old matrix):

dim(M) <- c(4,248,74)
mn <- apply(M, c(2,3), mean)

the following error occured:
Error: dim<- : dims [product 73408] do not match the
length of object [74]

When I manually checked the answers, it seems that
rowsum(mat, gl(nrow(mat)/4, 4)) / 4 gives me the
correct answer.

Thanks again for your time & suggestions.

Regards,
Ezhil




--- Dimitris Rizopoulos
<[hidden email]> wrote:

> yes you're right; it was my mistake.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
>    
>
http://www.student.kuleuven.be/~m0390867/dimitris.htm

>
>
> ----- Original Message -----
> From: "Rolf Turner" <[hidden email]>
> To: <[hidden email]>;
> <[hidden email]>
> Cc: <[hidden email]>
> Sent: Monday, May 29, 2006 1:55 PM
> Subject: Re: [R] newbie question: ROW average
>
>
> > Dimitris Rizopoulos wrote:
> >
> >> look at ?rowMeans; you can also use "apply(mat,
> 1, mean)" but
> >> rowMeans() is better.
> >
> > By my reading of the question, this is not what
> > Ezhil wants.  He said:
> >
> > ``I have a 992 x 74 matrix. I would like to form a
> new matrix
> >   by averaging each 4 rows from the original
> one.''
> >
> > I.e. he wants (I think) the first row of the new
> matrix
> > to be the mean of the first 4 rows of the old one,
> the
> > second row of the new matrix to be the mean of
> rows 5
> > through 8 of the old one, and so on.
> >
> > One way this could be done is via
> >
> > > m.new <-
>
t(apply(array(t(m.old),dim=c(74,4,992/4)),c(1,3),mean))

> >
> > cheers,
> >
> > Rolf Turner
> > [hidden email]
> >
>
> Disclaimer:
> http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: newbie question: ROW average

Peter Dalgaard
A Ezhil <[hidden email]> writes:

> When I tried with (assuming 'M' is my old matrix):
>
> dim(M) <- c(4,248,74)
> mn <- apply(M, c(2,3), mean)
>
> the following error occured:
> Error: dim<- : dims [product 73408] do not match the
> length of object [74]

In that case, M clearly wasn't a 992x74 matrix!

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html