Skip to content

Retain parts of a matrix

5 messages · David Winsemius, Katrina Bennett, Jim Lemon

#
On Nov 27, 2011, at 10:15 PM, Katrina Bennett wrote:

            
......................^
cdem.mat is not a function, cannot use "(".
You are only offering 10 values as replacements and an unequal number  
of condition regardless of whether you count the qualifying cdem  
conditions (6) or the total conditions(15). So you will need to be  
more clear about how you want the conditions to apply to values. They  
stepping the audience through the first few decisions and say where  
you want the values placed. Maybe you should also say whether you  
expect values to be recycled (the default with matrix assignment).

I suppose it is possible that you want the results of this:

cdem.mat[cdem>899] <-  t.mean.1.c - (cdem * -0.0065)

.... but I'm guessing not.
David Winsemius, MD
West Hartford, CT
#
On Nov 28, 2011, at 12:06 AM, Katrina Bennett wrote:

            
I have no idea what a DEM might be.
Construct a depth adjustment matrix:
 >   -0.0065 *matrix(cdem, 4,2)* (2*(cdem >= 900)-1)
       [,1]  [,2]
[1,]  1.95  1.95
[2,]  2.60  2.60
[3,]  4.55  4.55
[4,] -5.85 -5.85

Makes two copies of cdem in a column matrix, multiplies by -1 or 1  
depending on the condition, multiplies by you adjustment factor.  
Transposing it will properly oriented to subtract from t.mean.c.1  
using R matrix subtraction (which recycles by columns).

 > t.mean.1.c - t( -0.0065 *matrix(cdem, 4,2)* (2*(cdem >= 900)-1) )
        [,1]  [,2]   [,3]   [,4]
[1,] -15.95 -16.6 -18.55  -8.15
[2,] -21.95 -22.6 -24.55 -14.15


More complicated adjustment choices could be constructed with  
findInterval as an factor-choosing index. For your simple  problem  
case this would suffice..

 > c(0.0065, -0.0065)[findInterval(cdem, c(0,899,Inf))]
[1]  0.0065  0.0065  0.0065 -0.0065
I don't understand why you keep using get(). Just use the variable name.
If you stick with matrix operations, that's what you get.
David Winsemius, MD
West Hartford, CT
#
On 11/28/2011 04:06 PM, Katrina Bennett wrote:
Hi Katrina,
If cdem is a matrix (and it looks like a vector in your example), you 
could try:

le300<-function(elevation,temp) return(temp+elevation*0.0065)
gt300<-function(elevation,temp) return(temp-elevation*0.0065)
cdem[cdem<=300]<-le300(cdem[cdem<=300],t.mean.1.c[cdem<=300])
cdem[cdem>300]<-gt300(cdem[cdem>300],t.mean.1.c[cdem>300])

where le300 is the correction for altitudes less than or equal to 300M 
and gt300 is the one for altitudes greater than 300M. This works for me 
with a toy function. However, I don't get the same numbers as you and 
wonder if your function is doing the same as the ones I use above. Also, 
why 300m in the text and 900m in the functions? Are we mixing up feet 
and meters?

Jim