Quantcast

specific matrix element tranformation

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

specific matrix element tranformation

bantex
Hi guys,

I am trying to write a function that allows me to perform specific transformations to each element of
a 2 by 2 matrix to generate a 3 by 3 matrix.

Input into function-2 by 2 matrix
Output from function -3 by 3 matrix

For example:
# a is my original 2 by 2 matrix
#b is my new 3 by 3 matrix

set.seed(2)
a=matrix(rnorm(4),ncol=2)

#let's say these are the transformations i wish to perform
b[1,1]=a[1,1]+1
b[1,2]=a[2,1]*a[2,2]
b[1,3]=a[2,2]+a[1,1]
b[2,1]=a[1,1]-5
b[2,2]=sqrt(a[2,2])
b[2,3]=a[1,1]/a[2,2]
b[3,1]=a[2,2]-3
b[3,2]=a[1,1]*[a[1,2]+a[2,2])
b[3,3]=sqrt(a[1,1])/a[1,2])


Thanks.

Cheers,
B
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

bantex
Hi guys,

After a long while I came up with this :

set.seed(2)
a<-matrix(rnorm(4),ncol=2)
abc<-function(a)
{
b=matrix(NA,nrow=3,ncol=3)
b[1,1]=a[1,1]+1
b[1,2]=a[2,1]*a[2,2]
b[1,3]=a[2,2]+a[1,1]
b[2,1]=a[1,1]-5
b[2,2]=sqrt(a[2,2])
b[2,3]=a[1,1]/a[2,2]
b[3,1]=a[2,2]-3
b[3,2]=a[1,1]*(a[1,2]+a[2,2])
b[3,3]=sqrt(a[1,1])/a[1,2]

return(b=matrix(c(b[1,1],b[1,2],b[1,3],b[2,1],b[2,2],b[2,3],
b[3,1],b[3,2],b[3,3]),ncol=3))
}

How can I improve on it?

Cheers,
B
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

Rui Barradas
Hello,

Inline

Em 17-08-2012 04:39, bantex escreveu:

> Hi guys,
>
> After a long while I came up with this :
>
> set.seed(2)
> a<-matrix(rnorm(4),ncol=2)
> abc<-function(a)
> {
> b=matrix(NA,nrow=3,ncol=3)
> b[1,1]=a[1,1]+1
> b[1,2]=a[2,1]*a[2,2]
> b[1,3]=a[2,2]+a[1,1]
> b[2,1]=a[1,1]-5
> b[2,2]=sqrt(a[2,2])
> b[2,3]=a[1,1]/a[2,2]
> b[3,1]=a[2,2]-3
> b[3,2]=a[1,1]*(a[1,2]+a[2,2])
> b[3,3]=sqrt(a[1,1])/a[1,2]
>
> return(b=matrix(c(b[1,1],b[1,2],b[1,3],b[2,1],b[2,2],b[2,3],
> b[3,1],b[3,2],b[3,3]),ncol=3))

You don't need to say that 'b' is a matrix a second time! And this form
is wrong, you should add 'byrow = TRUE' since you're using row order.

> }
>
> How can I improve on it?

With your data, you would have two errors in applying function abc to
matrix 'a'. Try the following.

fun <- function(a){
     b <- matrix(nrow = 3, ncol = 3)
     #let's say these are the transformations i wish to perform
     b[1,1] <- a[1,1]+1
     b[1,2] <- a[2,1]*a[2,2]
     b[1,3] <- a[2,2]+a[1,1]
     b[2,1] <- a[1,1]-5
     if(a[2,2] < 0){
         warning("a[2, 2] is negative, using zero for its square root
b[2,2].")
         b[2,2] <- 0
     }else{
         b[2,2] <- sqrt(a[2,2])
     }
     if(a[2,2] == 0){
         warning("a[2, 2] is zero, using zero for division result b[2,3].")
         b[2,3] <- 0
     }else{
         b[2,3] <- a[1,1]/a[2,2]
     }
     b[3,1] <- a[2,2]-3
     b[3,2] <- a[1,1]*(a[1,2]+a[2,2])
     if(a[1,2] == 0){
         warning("a[1, 2] is zero, using zero for division result b[3,3].")
         b[3,3] <- 0
     }else if(a[1,1]/a[1,2] < 0){
         warning("a[1,1]/a[1,2] is negative, using zero for its square
root b[3,3].")
         b[3,3] <- 0
     }else{
         b[3,3] <- sqrt(a[1,1]/a[1,2])
     }
     b
}

set.seed(2)
a <- matrix(rnorm(4),ncol = 2)
fun(a)


>
> Cheers,
> B
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/specific-matrix-element-tranformation-tp4640550p4640554.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

David Winsemius
In reply to this post by bantex

On Aug 16, 2012, at 8:39 PM, bantex wrote:

> Hi guys,
>
> After a long while I came up with this :
>
> set.seed(2)
> a<-matrix(rnorm(4),ncol=2)
> abc<-function(a)
> {
> b=matrix(NA,nrow=3,ncol=3)
> b[1,1]=a[1,1]+1
> b[1,2]=a[2,1]*a[2,2]
> b[1,3]=a[2,2]+a[1,1]
> b[2,1]=a[1,1]-5
> b[2,2]=sqrt(a[2,2])
> b[2,3]=a[1,1]/a[2,2]
> b[3,1]=a[2,2]-3
> b[3,2]=a[1,1]*(a[1,2]+a[2,2])
> b[3,3]=sqrt(a[1,1])/a[1,2]
>
> # return(b=matrix(c(b[1,1],b[1,2],b[1,3],b[2,1],b[2,2],b[2,3],
> # b[3,1],b[3,2],b[3,3]),ncol=3))

  return(b)

> }
>
> How can I improve on it?
>
> Cheers,
> B
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/specific-matrix-element-tranformation-tp4640550p4640554.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

Rui Barradas
In reply to this post by Rui Barradas
Hello,

I've made a mistake, it's in the last if/else/if/else. Complete
statement below.

Em 17-08-2012 06:23, Rui Barradas escreveu:

> Hello,
>
> Inline
>
> Em 17-08-2012 04:39, bantex escreveu:
>> Hi guys,
>>
>> After a long while I came up with this :
>>
>> set.seed(2)
>> a<-matrix(rnorm(4),ncol=2)
>> abc<-function(a)
>> {
>> b=matrix(NA,nrow=3,ncol=3)
>> b[1,1]=a[1,1]+1
>> b[1,2]=a[2,1]*a[2,2]
>> b[1,3]=a[2,2]+a[1,1]
>> b[2,1]=a[1,1]-5
>> b[2,2]=sqrt(a[2,2])
>> b[2,3]=a[1,1]/a[2,2]
>> b[3,1]=a[2,2]-3
>> b[3,2]=a[1,1]*(a[1,2]+a[2,2])
>> b[3,3]=sqrt(a[1,1])/a[1,2]
>>
>> return(b=matrix(c(b[1,1],b[1,2],b[1,3],b[2,1],b[2,2],b[2,3],
>> b[3,1],b[3,2],b[3,3]),ncol=3))
>
> You don't need to say that 'b' is a matrix a second time! And this
> form is wrong, you should add 'byrow = TRUE' since you're using row
> order.
>
>> }
>>
>> How can I improve on it?
>
> With your data, you would have two errors in applying function abc to
> matrix 'a'. Try the following.
>
> fun <- function(a){
>     b <- matrix(nrow = 3, ncol = 3)
>     #let's say these are the transformations i wish to perform
>     b[1,1] <- a[1,1]+1
>     b[1,2] <- a[2,1]*a[2,2]
>     b[1,3] <- a[2,2]+a[1,1]
>     b[2,1] <- a[1,1]-5
>     if(a[2,2] < 0){
>         warning("a[2, 2] is negative, using zero for its square root
> b[2,2].")
>         b[2,2] <- 0
>     }else{
>         b[2,2] <- sqrt(a[2,2])
>     }
>     if(a[2,2] == 0){
>         warning("a[2, 2] is zero, using zero for division result
> b[2,3].")
>         b[2,3] <- 0
>     }else{
>         b[2,3] <- a[1,1]/a[2,2]
>     }
>     b[3,1] <- a[2,2]-3
>     b[3,2] <- a[1,1]*(a[1,2]+a[2,2])
>     if(a[1,2] == 0){
>         warning("a[1, 2] is zero, using zero for division result
> b[3,3].")
>         b[3,3] <- 0
>     }else if(a[1,1]/a[1,2] < 0){
>         warning("a[1,1]/a[1,2] is negative, using zero for its square
> root b[3,3].")
>         b[3,3] <- 0
>     }else{
>         b[3,3] <- sqrt(a[1,1]/a[1,2])
>     }
>     b
> }
>
> set.seed(2)
> a <- matrix(rnorm(4),ncol = 2)
> fun(a)
>
>

Correction:

     if(a[1,2] == 0){
         warning("a[1, 2] is zero, using zero for division result b[3,3].")
         b[3,3] <- 0
     }else if(a[1,1] < 0){
         warning("a[1, 1] is negative, using zero for its square root
b[3,3].")
         b[3,3] <- 0
     }else{
         b[3,3] <- sqrt(a[1,1])/a[1,2]
     }

I thought it was sqrt(a[1,1]/a[1,2]).
And apparently I've also forgot to sign.

Rui Barradas

>>
>> Cheers,
>> B
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/specific-matrix-element-tranformation-tp4640550p4640554.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [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
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

bantex
In reply to this post by David Winsemius
Thanks David. I have actually 1 more question. How do I assign a name to the output created by this function?

Cheers,
B
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

David Winsemius

On Aug 17, 2012, at 6:04 PM, bantex wrote:

> Thanks David. I have actually 1 more question. How do I assign a  
> name to the
> output created by this function?
>

abc_ret <- abc(...)

> --
> View this message in context: http://r.789695.n4.nabble.com/specific-matrix-element-tranformation-tp4640550p4640680.html
> Sent from the R help mailing list archive at Nabble.com.

Rhelp is a mailing list. It is NOT Nabble. You are asked in the  
Posting Guide to include context.
>
 >>>>>>>>>>>>>>>>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 >>>>>>>>>>>>>>>

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: specific matrix element tranformation

Michael Weylandt
In reply to this post by bantex
Please do include context.

"name assignment" can mean a few things:

i) Adding a "name" attribute can be done with names(x) <- _whatever_
ii) Assigning to an object name should be done outside the function,
not within it:

x <- myLongFunc(...)

Cheers,
Michael

On Fri, Aug 17, 2012 at 9:04 PM, bantex <[hidden email]> wrote:

> Thanks David. I have actually 1 more question. How do I assign a name to the
> output created by this function?
>
> Cheers,
> B
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/specific-matrix-element-tranformation-tp4640550p4640680.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Loading...