Hello,
I'm at the very beginning of the learning process of this language.
Sorry in advance for the (possible but plausible) stupidity of my question.
I would like to find a way to permute the DIMENSIONS of an array.
Something that sounds like the function "permute()" in matlab.
Given an array C of dimensions c x d x T , for instance, the command
permute(C, [2 1 3])
would provide (in Matlab) an array very similar to C, but this time each
one of the T matrices c x d has changed into its transposed.
Any alternatives to the following (and primitive) 'for' cycle?
*# (previously defined) phi=array with dimensions c(c,d,T)*
*
*
*temp=array(0,dim=c(c,d,T))*
* for(i in 1:T)*
* {*
* temp[,,i]=t(phi[,,i])*
* }*
* phi=temp*
*
*
Thank you very much!
S
--
-------------------------------------------------------------------
Simone Salvadei
Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salvadei@uniroma2.it <federico.belotti@uniroma2.it>
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
<http://www.econometrics.it/>
-------------------------------------------------------------------
[[alternative HTML version deleted]]
On Nov 2, 2011, at 12:16 PM, Simone Salvadei wrote:> Hello, > I'm at the very beginning of the learning process of this language. > Sorry in advance for the (possible but plausible) stupidity of my > question. > > I would like to find a way to permute the DIMENSIONS of an array. > Something that sounds like the function "permute()" in matlab. > > Given an array C of dimensions c x d x T , for instance, the command > > permute(C, [2 1 3]) >?aperm> would provide (in Matlab) an array very similar to C, but this time > each > one of the T matrices c x d has changed into its transposed. > Any alternatives to the following (and primitive) 'for' cycle? > > *# (previously defined) phi=array with dimensions c(c,d,T)* > * > * > *temp=array(0,dim=c(c,d,T))* > * for(i in 1:T)* > * {* > * temp[,,i]=t(phi[,,i])* > * }* > * phi=temp* > * > *David Winsemius, MD Heritage Laboratories West Hartford, CT
You seem to be looking for 'aperm'. There is a chapter in 'S Poetry' (available on http://www.burns-stat.com) that talks about working with higher dimensional arrays. I don't think any changes need to be made for R. On 02/11/2011 16:16, Simone Salvadei wrote:> Hello, > I'm at the very beginning of the learning process of this language. > Sorry in advance for the (possible but plausible) stupidity of my question. > > I would like to find a way to permute the DIMENSIONS of an array. > Something that sounds like the function "permute()" in matlab. > > Given an array C of dimensions c x d x T , for instance, the command > > permute(C, [2 1 3]) > > would provide (in Matlab) an array very similar to C, but this time each > one of the T matrices c x d has changed into its transposed. > Any alternatives to the following (and primitive) 'for' cycle? > > *# (previously defined) phi=array with dimensions c(c,d,T)* > * > * > *temp=array(0,dim=c(c,d,T))* > * for(i in 1:T)* > * {* > * temp[,,i]=t(phi[,,i])* > * }* > * phi=temp* > * > * > > Thank you very much! > S >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
This worked example, hoping to be helpful, has been requested after a (my)
further enquiry about array manipulation.
I was looking for a command that is equivalent to repmat() in matlab and
that could also be applied to array.
(for Matlab users)
The Matlal code was the following:
temp_u=zeros(d,c,T); <-creation of an array of dimensions d x
c x T full of zeroes
temp_u(1,:,:)=m_u; <-filling the first row of each
'stratum'
with the rows of
the matrix 'm_u'
temp_u=repmat(temp_u(1,:,:),d,1); <-filling the remaining rows (full of
zeroes) of 'temp_u' with
copies of the corrensponding 1st row
--
-------------------------------------------------------------------
Simone Salvadei
Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salvadei@uniroma2.it <federico.belotti@uniroma2.it>
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
<http://www.econometrics.it/>
-------------------------------------------------------------------
[[alternative HTML version deleted]]
This worked example, hoping to be helpful, has been requested after a (my)
further enquiry about array manipulation.
I was looking for a command that is equivalent to repmat() in matlab and
that could also be applied to array.
(for Matlab users)
The Matlal code was the following:
1)temp_u=zeros(d,c,T); <-creation of an array of dimensions d
x c x T full of zeroes
2)temp_u(1,:,:)=m_u; <-filling the first row of each
'stratum' with the rows of
the matrix 'm_u'
3)temp_u=repmat(temp_u(1,:,:),d,1); <-filling the remaining rows (full of
zeroes) of 'temp_u' with
copies of the corrensponding 1st row
(what's happening if you are not a Matlab users)
(numerical example d=2,c=4,T=4)
1)temp_u=zeros(d,c,T)
temp_u(:,:,1)
0 0 0 0
0 0 0 0
temp_u(:,:,2)
0 0 0 0
0 0 0 0
temp_u(:,:,3)
0 0 0 0
0 0 0 0
temp_u(:,:,4)
0 0 0 0
0 0 0 0
2)temp_u(1,:,:)=m_u
temp_u(:,:,1)
0.9604 0.0156 0.0230 0.0009
0 0 0 0
temp_u(:,:,2)
0.3906 0.2948 0.0981 0.2165
0 0 0 0
temp_u(:,:,3)
0.5390 0.2482 0.1140 0.0988
0 0 0 0
temp_u(:,:,4)
0.4546 0.2641 0.0794 0.2019
0 0 0 0
3)temp_u=repmat(temp_u(1,:,:),d,1)
temp_u(:,:,1)
0.9604 0.0156 0.0230 0.0009
0.9604 0.0156 0.0230 0.0009
temp_u(:,:,2)
0.3906 0.2948 0.0981 0.2165
0.3906 0.2948 0.0981 0.2165
temp_u(:,:,3)
0.5390 0.2482 0.1140 0.0988
0.5390 0.2482 0.1140 0.0988
temp_u(:,:,4)
0.4546 0.2641 0.0794 0.2019
0.4546 0.2641 0.0794 0.2019
Now, in order to reply this exercise in R I used the following code:
temp_u=array(0,dim=c(1,c,T))
temp_u[1,,]=m_u
temp_u=kronecker(temp_u,matrix(rep(1,d),nr=d))
A special thank to David Winsemius,William Dunlap and Patrick Burns.
I hope I have been helpful.
-------------------------------------------------------------------
Simone Salvadei
Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salvadei@uniroma2.it <federico.belotti@uniroma2.it>
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
<http://www.econometrics.it/>
-------------------------------------------------------------------
[[alternative HTML version deleted]]