mercredi 2 mars 2016

if-statement concerning restrictions greater/smaller

I want to programm an if-statement which restricts an matrix to the interval (-1) to 1.

input:

alpha=matrix(c(-2,0.5,3,0.2,0.8,0.5,0.2,0.8,0.1),3,3)

so it results in

alpha>1
    [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE FALSE
[3,]  TRUE FALSE FALSE

alpha<(-1)
    [,1]  [,2]  [,3]
[1,]  TRUE FALSE FALSE
[2,] FALSE FALSE FALSE
[3,] FALSE FALSE FALSE

How can I now programm a restriction which sets the values which are TRUE here to 0.99 and -0.99 like

if(alpha<(-1)){alpha[]=-0.99}
if(alpha>1){alpha[]=0.99}

so that the output of alpha is

     [,1] [,2] [,3]
[1,] -0.99  0.2  0.2
[2,]  0.50  0.8  0.8
[3,]  0.99  0.5  0.1

How to programm the (if) condition?

Aucun commentaire:

Enregistrer un commentaire