sustraction of two vectors of matrix

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

sustraction of two vectors of matrix

R help mailing list-2
helloplease   I want to make a sustration of two vectors of a matrix
i have this program

aa<-matrix(outer(0:3,0:4,function(x,y) x+y*2),nrow=4,ncol=5)
for(i in 1:4)
+ {for(j in 2:5)
+ {bb[i,j-1]=aa[i,j]-aa[i,j-1]
+ }
+ }
at the end i obtain the bb=matrix( nrow=4,ncol=4)
but i cann't obtain this matrix
thank you very much




        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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
|

Re: sustraction of two vectors of matrix

Rui Barradas
Hello,

1) You don't need matrix(outer(etc)), outer already returns a matrix.
2) You need to create bb first.

aa <- outer(0:3, 0:4, function(x,y) x + y*2)

bb <- matrix(nrow = 4, ncol = 4)

for(i in 1:4){
   for(j in 2:5){
     bb[i, j - 1] <- aa[i, j] - aa[i, j - 1]
   }
}

bb


Hope this helps,

Rui Barradas


Às 14:30 de 05/12/2018, malika yassa via R-help escreveu:

> helloplease   I want to make a sustration of two vectors of a matrix
> i have this program
>
> aa<-matrix(outer(0:3,0:4,function(x,y) x+y*2),nrow=4,ncol=5)
> for(i in 1:4)
> + {for(j in 2:5)
> + {bb[i,j-1]=aa[i,j]-aa[i,j-1]
> + }
> + }
> at the end i obtain the bb=matrix( nrow=4,ncol=4)
> but i cann't obtain this matrix
> thank you very much
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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
|

Re: sustraction of two vectors of matrix

Jeff Newmiller
or with no loops and no preallocation:

bb <- aa[ , 2:5 ] - aa[ , 1:4 ]


On December 5, 2018 8:51:16 AM PST, Rui Barradas <[hidden email]> wrote:

>Hello,
>
>1) You don't need matrix(outer(etc)), outer already returns a matrix.
>2) You need to create bb first.
>
>aa <- outer(0:3, 0:4, function(x,y) x + y*2)
>
>bb <- matrix(nrow = 4, ncol = 4)
>
>for(i in 1:4){
>   for(j in 2:5){
>     bb[i, j - 1] <- aa[i, j] - aa[i, j - 1]
>   }
>}
>
>bb
>
>
>Hope this helps,
>
>Rui Barradas
>
>
>Às 14:30 de 05/12/2018, malika yassa via R-help escreveu:
>> helloplease   I want to make a sustration of two vectors of a matrix
>> i have this program
>>
>> aa<-matrix(outer(0:3,0:4,function(x,y) x+y*2),nrow=4,ncol=5)
>> for(i in 1:4)
>> + {for(j in 2:5)
>> + {bb[i,j-1]=aa[i,j]-aa[i,j-1]
>> + }
>> + }
>> at the end i obtain the bb=matrix( nrow=4,ncol=4)
>> but i cann't obtain this matrix
>> thank you very much
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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 -- To UNSUBSCRIBE and more, see
>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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.