[I am really sorry if it is double posted, I doubt me previous post could not reach forum due to some problem with net] Suppose I have a matrix : a = matrix(1:9, 3)>From this matrix, I construct 9 additional matrices :i = 1:9 bi = a * i Now combining all those 9 new matrices, I construct a final metrix as : c = b1 b4 b7 b2 b5 b8 b3 b6 b8 I want to automate this procedure for any arbitrary number "i" Can anyone please help me how to do that? Rgd, -- View this message in context: http://www.nabble.com/A-matrix-automation-problem-tp20106932p20106932.html Sent from the R help mailing list archive at Nabble.com.
megh <megh700004 <at> yahoo.com> writes:> > > [I am really sorry if it is double posted, I doubt me previous post could not > reach forum due to some problem with net] > > Suppose I have a matrix : > > a = matrix(1:9, 3) > > >From this matrix, I construct 9 additional matrices : > > i = 1:9 > bi = a * i > > Now combining all those 9 new matrices, I construct a final metrix as : > > c = > > b1 b4 b7 > b2 b5 b8 > b3 b6 b8 > > I want to automate this procedure for any arbitrary number "i" >It may be too specialized, but it sounds like you're looking for a Kronecker product. a = matrix(1:9,3)> kronecker(a,a)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 4 7 4 16 28 7 28 49 [2,] 2 5 8 8 20 32 14 35 56 [3,] 3 6 9 12 24 36 21 42 63 [4,] 2 8 14 5 20 35 8 32 56 [5,] 4 10 16 10 25 40 16 40 64 [6,] 6 12 18 15 30 45 24 48 72 [7,] 3 12 21 6 24 42 9 36 63 [8,] 6 15 24 12 30 48 18 45 72 [9,] 9 18 27 18 36 54 27 54 81 Ben Bolker
Does this help point you in a useful direction?> a <- matrix(1:9, 3) * 10 > b <- matrix(1:9, 3) > kronecker(b,a)-- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of megh > Sent: Wednesday, October 22, 2008 3:22 AM > To: r-help at r-project.org > Subject: [R] A matrix automation problem > > > [I am really sorry if it is double posted, I doubt me previous post > could not > reach forum due to some problem with net] > > Suppose I have a matrix : > > a = matrix(1:9, 3) > > >From this matrix, I construct 9 additional matrices : > > i = 1:9 > bi = a * i > > Now combining all those 9 new matrices, I construct a final metrix as : > > c > > b1 b4 b7 > b2 b5 b8 > b3 b6 b8 > > I want to automate this procedure for any arbitrary number "i" > > Can anyone please help me how to do that? > > Rgd, > > > -- > View this message in context: http://www.nabble.com/A-matrix- > automation-problem-tp20106932p20106932.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Thanks for your reply. Your solution is ok for this particular definition of bi = a * i . However I was looking for more general solution, like : bi = a *a*a*....(i-times) Is there any better idea? Thanks and regards, Greg Snow-2 wrote:> > Does this help point you in a useful direction? > >> a <- matrix(1:9, 3) * 10 >> b <- matrix(1:9, 3) >> kronecker(b,a) > > > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.snow at imail.org > 801.408.8111 > > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> project.org] On Behalf Of megh >> Sent: Wednesday, October 22, 2008 3:22 AM >> To: r-help at r-project.org >> Subject: [R] A matrix automation problem >> >> >> [I am really sorry if it is double posted, I doubt me previous post >> could not >> reach forum due to some problem with net] >> >> Suppose I have a matrix : >> >> a = matrix(1:9, 3) >> >> >From this matrix, I construct 9 additional matrices : >> >> i = 1:9 >> bi = a * i >> >> Now combining all those 9 new matrices, I construct a final metrix as : >> >> c >> >> b1 b4 b7 >> b2 b5 b8 >> b3 b6 b8 >> >> I want to automate this procedure for any arbitrary number "i" >> >> Can anyone please help me how to do that? >> >> Rgd, >> >> >> -- >> View this message in context: http://www.nabble.com/A-matrix- >> automation-problem-tp20106932p20106932.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting- >> guide.html >> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/A-matrix-automation-problem-tp20106932p20123993.html Sent from the R help mailing list archive at Nabble.com.
megh: assuming that i understand what you want, i think below does it
but check it carefully because i didn't.
i = 1:9
a = matrix(1:9, 3)
tempa <- lapply(i,function(.index) {
a^.index
})
tempb <- lapply(seq(1,dim(a)[1]*dim(a)[2],by=dim(a)[1]),function(.index)
{
do.call(rbind,tempa[.index:(.index+(dim(a)[1])-1)])
})
tempc <- do.call(cbind,tempb)
print(tempc)
print(str(tempc))
On Wed, Oct 22, 2008 at 11:57 PM, megh wrote:
> Thanks for your reply. Your solution is ok for this particular
> definition of
> bi = a * i
> . However I was looking for more general solution, like :
> bi = a *a*a*....(i-times)
>
> Is there any better idea?
> Thanks and regards,
>
>
> Greg Snow-2 wrote:
>>
>> Does this help point you in a useful direction?
>>
>>> a <- matrix(1:9, 3) * 10
>>> b <- matrix(1:9, 3)
>>> kronecker(b,a)
>>
>>
>>
>> --
>> Gregory (Greg) L. Snow Ph.D.
>> Statistical Data Center
>> Intermountain Healthcare
>> greg.snow at imail.org
>> 801.408.8111
>>
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>>> project.org] On Behalf Of megh
>>> Sent: Wednesday, October 22, 2008 3:22 AM
>>> To: r-help at r-project.org
>>> Subject: [R] A matrix automation problem
>>>
>>>
>>> [I am really sorry if it is double posted, I doubt me previous post
>>> could not
>>> reach forum due to some problem with net]
>>>
>>> Suppose I have a matrix :
>>>
>>> a = matrix(1:9, 3)
>>>
>>>> From this matrix, I construct 9 additional matrices :
>>>
>>> i = 1:9
>>> bi = a * i
>>>
>>> Now combining all those 9 new matrices, I construct a final metrix
>>> as :
>>>
>>> c >>>
>>> b1 b4 b7
>>> b2 b5 b8
>>> b3 b6 b8
>>>
>>> I want to automate this procedure for any arbitrary number
"i"
>>>
>>> Can anyone please help me how to do that?
>>>
>>> Rgd,
>>>
>>>
>>> --
>>> View this message in context: http://www.nabble.com/A-matrix-
>>> automation-problem-tp20106932p20106932.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-
>>> guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> --
> View this message in context:
> http://www.nabble.com/A-matrix-automation-problem-tp20106932p20123993.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.