[R] Problems dealing with matrices
arun
smartpink111 at yahoo.com
Thu Nov 28 17:11:20 CET 2013
Hi Halim,
For the first two questions, you may try:
colsum1 <- colSums(volyrdc1)
min(which(colsum1>=18))
#[1] 29
#or
head(which(colsum1>=18),1)
#140
# 29
colsum1[substr(colsum1,6,7)=="00"] ## this is not very clear
305
45.37004
#or
colsum1[colsum1>=18][substr(colsum1[colsum1>=18],6,7)=="00"]
305
45.37004
#because
sprintf("%.4f",colsum1[colsum1>=18])
colsum1[colsum1>=18][gsub(".*\\.\\d{2}","",sprintf("%.4f",colsum1[colsum1>=18]))=="00"]
180 305
32.88996 45.37004
A.K.
On Thursday, November 28, 2013 3:57 AM, halim10-fes <halim10-fes at sust.edu> wrote:
Hi,
Sorry for continuous bothering. Continuum of the previous problem...
I have the following matrices and vectors,
dcmat<-matrix(c(0.13,0.61,0.25,0.00,0.00,0.00,0.52,0.37,0.09,0.00,0.00,0.00,
0.58,0.30,0.11,0.00,0.00,0.00,0.46,0.22,0.00,0.00,0.00,0.00,
0.09),nrow=5,ncol=5)
volini<-matrix(c(0,0,0,0,0),nrow=5,ncol=1)
volinp1<-c(0, 0.0004669094, 0.0027610861, 0.0086204692, 0.0200137754,
0.0389069106 ,0.0670942588, 0.1060941424, 0.1570990708, 0.2209672605,
0.2982420945, 0.3891882830, 0.4938361307, 0.6120278338, 0.7434618363,
0.8877329008, 1.0443667375, 1.2128488387, 1.3926476912, 1.5832328410,
1.7840884399, 1.9947229566, 2.2146757191, 2.4435209092, 2.6808695568,
2.9263700050, 3.1797072430, 3.4406014299, 3.7088058696, 3.9841046430,
4.2663100561, 4.5552600226, 4.8508154713, 5.1528578389, 5.4612866929,
5.7760175114, 6.0969796345, 6.4241143947, 6.7573734248, 7, 7 ,7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7)
I've calculated the following matrices vol and volyrdc1 (obviously with the
help of Jeff and Arun):
#Blank matrices for dumping final values
vol <- matrix( NA, nrow=5, ncol=length(volinp1))
volyrdc1<-matrix(NA, nrow=5,ncol=length(volinp1),dimnames=
list(c("DC1","DC2","DC3","DC4","DC5"),c(seq(0,500,5))))
vol[ , 1 ] <- dcmat %*% (volini+(volinp1[1]*wt))
wt<-matrix(c(1,0,0,0,0),nrow=5)
for ( idx in seq_along(volinp1)[ -1 ] ) {
vol[ , idx ] <- dcmat %*% ( vol[ , idx-1 ] + volinp1[idx] * wt )
}
vol
volyrdc1[,1]<-vol[,1]
for ( idx in seq_along(volinp1)[ -1 ] ) {
volyrdc1[ , idx ] <- vol[ , idx-1 ] + volinp1[idx] * wt
}
volyrdc1
My final matrix in 'volyrdc1' (kind of transition matrix model).
Now, what I want to do is to calculate when the colsum<-colSums(volyrdc1)
reaches a certain value and I want to get the index of the element in the
'colsum' vector at that point. For e.g. when colsum[colsum>=18] ? It will give
a series of cases where the condition is true. But I want index of the element
immediately when the condition is met. In this case, the answer I want is 140
(colsum[29] returns both value (18.63) and the character ("140") attributing
the index). Actually, in my case 140 is year (age) when the 'colsum' becomes
>=18. At is point it would be great if I can calculate when 'colsum' levels
off (up to two decimal place)? The answer is: 305 and at that point
colsum==45.37.
I also want to calculate what should be the value in volini[1,1] to get a
certain value in 'colsum' at a certain year (age)(vector element index
explained earlier)? For e.g. I want to find out that what should be the value
in volini[1,1] if I want colsum==18 at 100(charater attributing colsum[21])?
The answer is: 15910 and the 'volini' matrix will look like:
volini<-matrix(c(15910,0,0,0,0),nrow=5,ncol=1)
Any pointer, suggestions,... will be gratefully acknowledged.
P.S. Can you please suggest me any effective R programming book that describe
core elements of R programming?
Thanks in advance.
Regards,
Halim
---------------
Md. Abdul Halim
Assistant Professor
Department of Forestry and Environmental Science
Shahjalal University of Science and Technology,Sylhet-3114,
Bangladesh.
Cell: +8801714078386.
alt. e-mail: xou03 at yahoo.com
On Tue, 26 Nov 2013 20:21:14 -0800 (PST), arun wrote
> HI Halim,
>
> No problem.
> Regards,
> Arun
>
> On Tuesday, November 26, 2013 11:18 PM, halim10-fes <halim10-
> fes at sust.edu> wrote: Hi Arun,
>
> Thanks for your help. Sorry for my late response. Take care and stay
> fine.
>
> Regards,
>
> Halim
>
> On Sun, 24 Nov 2013 07:45:24 -0800 (PST), arun wrote
> > Hi Halim,
> > I guess this works for you. Modifying Jeff's solution:
> >
> > volinp<-c(0,0.000467,0.002762,0.008621,0.020014,0.038907,0.067094)
> > vol1 <- dcmat %*% (volmat +wt)
> > for(idx in seq_along(volinp)[-1]){
> > vol1 <- cbind(vol1,dcmat %*% (vol1[,idx-1] + volinp[idx] *wt))
> > }
> >
> > #or
> >
> > vol <- matrix( NA, nrow=5, ncol=length( volinp ) )
> > vol[ , 1 ] <- dcmat %*% ( volmat + wt )
> >
> > for ( idx in seq_along(volinp)[ -1 ] ) {
> > vol[ , idx ] <- dcmat %*% ( vol[ , idx-1 ] + volinp[idx] * wt )
> > }
> > identical(vol,vol1)
> > #[1] TRUE
> >
> > A.K.
> >
> > On Sunday, November 24, 2013 7:16 AM, halim10-fes <halim10-
> > fes at sust.edu> wrote: Hi Arun,
> >
> > OK, no problem. Thank you very much for your attention. I've posted
> > an annex to my previous problem. I will appreciate your
> > comments/suggestions on it.
> >
> > Off-topic: You're a very helpful man. I like your attitude to
> > helping others.
> >
> > Take care.
> >
> > Halim
> >
> > On Sun, 24 Nov 2013 01:18:18 -0800 (PST), arun wrote
> > > Hi,
> > > Please disregard my earlier message. Looks like Jeff understand it
> > > better and answered it. Regards, Arun
> > >
> > > On Sunday, November 24, 2013 3:23 AM, arun <smartpink111 at yahoo.com>
wrote:
> > > Hi,
> > > I am finding some inconsistency with your description.
> > > For example:
> > > volinp[1]+volmat[1,1]
> > > [1] 101
> > >
> > > On Sunday, November 24, 2013 1:52 AM, halim10-fes <halim10-
> > > fes at sust.edu> wrote:
> > >
> > > Please apologize me! Earlier I've sent a message erroneously.
> > > Following is the original problem for which I'm seeking help.
> > > Extremely sorry...
> > >
> > > Hi Arun,
> > >
> > > Thank you very much for your response. Sorry, if I couldn't explain
> > > clearly. I think, I should restate the problem to get exactly what I
> > > want. Here it goes:
> > >
> > > I have 2 matrices and 1 vector, namely,
> > >
> > > dcmat<-matrix(c(0.13,0.61,0.25,0.00,0.00,0.00,0.52,0.37,0.09,0.00,
> > > 0.00,0.00, 0.58,0.30,0.11,0.00,0.00,0.00,0.46,0.22,
> > > 0.00,0.00,0.00,0.00, 0.09),nrow=5,ncol=5)
> > >
> > > volmat<-matrix(c(100,0,0,0,0),nrow=5,ncol=1)
> > >
> > > volinp<-c(1:40)
> > >
> > > What I essentially want to do is to multiply 'dcmat' with 'volmat'
> > > and dump the output in a new matrix 'vol'. But before that, in the
> > > first step, I want to add volinp[1] with volmat[1,1]. So, the first
> > > column of the output matrix 'vol' matrix will be:
> > >
> > > [,1]
> > > [1,] 13.13
> > > [2,] 61.61
> > > [3,] 25.25
> > > [4,] 0.00
> > > [5,] 0.00
> > >
> > > In the 2nd step, I want to replace 'volmat' with vol[,1] and add
> > > volinp[2] with vol[1,1]. The new 'volmat' will look like:
> > >
> > > [,1]
> > > [1,] 15.13
> > > [2,] 61.61
> > > [3,] 25.25
> > > [4,] 0.00
> > > [5,] 0.00
> > >
> > > Then multiply 'dcmat' with the new 'volmat', and the 2nd column of
> > > output matrix 'vol' will look like:
> > >
> > > [,2]
> > > [1,] 1.9669
> > > [2,] 41.2665
> > > [3,] 41.2232
> > > [4,] 13.1199
> > > [5,] 2.7775
> > >
> > > Then again, replace the 'volmat' with vol[,2], add volinp[3] with
> > > vol[1,2] and multiply the new 'volmat' with 'dcmat'. This
> > > replacement, addition, multiplication, and dumping will continue up
> > > to the length of 'volinp' and the final output matrix 'vol' will be
> > > something like:
> > >
> > > [,1] [,2] [,3] ...length(volinp)
> > > [1,] 13.13 1.9669 0.645697 ...
> > > [2,] 61.61 41.2665 24.488389 ...
> > > [3,] 25.25 41.2232 40.419786 ...
> > > [4,] 0.00 13.1199 22.116099 ...
> > > [5,] 0.00 2.7775 7.670905 ...
> > >
> > > Within my limited capacity, I've tried to come up with a solution
> > > but failed.
> > >
> > > I'll appreciate your/others' help with gratefulness.
> > >
> > > Regards,
> > >
> > > Halim
> > >
> > > ---------------
> > > Md. Abdul Halim
> > > Assistant Professor
> > > Department of Forestry and Environmental Science
> > > Shahjalal University of Science and Technology,Sylhet-3114,
> > > Bangladesh.
> > > Cell: +8801714078386.
> > > alt. e-mail: xou03 at yahoo.com
> > >
> > > On Sat, 23 Nov 2013 14:10:12 -0800 (PST), arun wrote
> > > > Hi,
> > > > Could you show your expected output? It is a bit unclear from the
> > > description.
> > > >
> > > > On Saturday, November 23, 2013 2:00 PM, halim10-fes <halim10-
> > > > fes at sust.edu> wrote: Dear R-friends,
> > > >
> > > > Hope you doing well. I've been trying to deal with the following
> > > > problem for the couple of days but couldn't come up with a solution.
> > > > It would be great if any of you could give some insight into it.
> > > >
> > > > I have three matrices like:
> > > >
> > > > dcvol<-matrix(c(0.13,0.61,0.25,0.00,0.00,0.00,0.52,0.37,0.09,0.00,
> > > > 0.00,0.00, 0.58,0.30,0.11,0.00,0.00,0.00,0.46,0.22,
> > > > 0.00,0.00,0.00,0.00, 0.09),nrow=5,ncol=5)
> > > volinp<-
> > > > matrix(c(100,0,0,0,0),nrow=5,ncol=1)
> > > >
> > > > scvol<-matrix(c(1:40),nrow=5,ncol=8)
> > > >
> > > > What I essentially want to do is to add each value in scvol[1,] with
> > > > the volinp[1,1] and then multiply each new volinp with dcvol and
> > > > finally put the outputs in a new matrix.
> > > >
> > > > Thanks in advance.
> > > >
> > > > Halim
> > > > ---------------
> > > > Md. Abdul Halim
> > > > Assistant Professor
> > > > Department of Forestry and Environmental Science
> > > > Shahjalal University of Science and Technology,Sylhet-3114,
> > > > Bangladesh.
> > > > Cell: +8801714078386.
> > > > alt. e-mail: xou03 at yahoo.com
> > > >
> > > > --
> > > > This message has been scanned for viruses and
> > > > dangerous content by MailScanner, and is
> > > > believed to be clean.
> > > >
> > > > ______________________________________________
> > > > 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.
> > >
> > > --
> > > This message has been scanned for viruses and
> > > dangerous content by MailScanner, and is
> > > believed to be clean.
> > >
> > > --
> > > This message has been scanned for viruses and
> > > dangerous content by MailScanner, and is
> > > believed to be clean.
>
> >
> > ---------------
> > Md. Abdul Halim
> > Assistant Professor
> > Department of Forestry and Environmental Science
> > Shahjalal University of Science and Technology,Sylhet-3114,
> > Bangladesh.
> > Cell: +8801714078386.
> > alt. e-mail: xou03 at yahoo.com
> >
> > --
> > This message has been scanned for viruses and
> > dangerous content by MailScanner, and is
> > believed to be clean.
> >
> > --
> > This message has been scanned for viruses and
> > dangerous content by MailScanner, and is
> > believed to be clean.
>
> ---------------
> Md. Abdul Halim
> Assistant Professor
> Department of Forestry and Environmental Science
> Shahjalal University of Science and Technology,Sylhet-3114,
> Bangladesh.
> Cell: +8801714078386.
> alt. e-mail: xou03 at yahoo.com
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
---------------
Md. Abdul Halim
Assistant Professor
Department of Forestry and Environmental Science
Shahjalal University of Science and Technology,Sylhet-3114,
Bangladesh.
Cell: +8801714078386.
alt. e-mail: xou03 at yahoo.com
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
More information about the R-help
mailing list