# [R] Continuous columns of matrix

Fri May 24 16:59:50 CEST 2013

```Hello,

Something like this?

fun2 <- function(x){
n <- length(x)
imx <- which.max(x)
if(imx == 1){
x[2] <- x[n] <- Inf
}else if(imx == n){
x[1] <- x[n - 1] <- Inf
}else{
x[imx - 1] <- Inf
x[imx + 1] <- Inf
}
index <- which(x <= 0.8*x[imx])
values <- x[index]
list(index = index, values = values)
}

apply(mat, 2, fun2)

Em 24-05-2013 13:40, eliza botto escreveu:
> Dear Rui,Thankyou very much for your help. just for my own knowledge what if want the values and index, which are less than or equal to 80% of the maximum value other than those in the neighbors?? like if maximum is in row number 5 of any column then the second maximum can be in any row other than 4 and 6. similarly if maximum is in row number 12 than the second maximum can be in any row other than 1 and 11...thankyou very much for your help
> elisa
>
>> Date: Fri, 24 May 2013 12:37:37 +0100
>> To: eliza_botto at hotmail.com
>> CC: bhh at xs4all.nl; r-help at r-project.org
>> Subject: Re: [R] Continuous columns of matrix
>>
>> Hello,
>>
>> Berend is right, it's at least confusing. To get just the index of the
>> maximum value in each column,
>>
>> apply(mat, 2, which.max)
>>
>>
>> To get that index and the two neighbours (before and after, wraping
>> around) if they are greater than or equal to 80% of the maximum, try
>>
>> fun <- function(x){
>> 	n <- length(x)
>> 	imx <- which.max(x)
>> 	sec <- numeric(2)
>> 	if(imx == 1){
>> 		if(x[n] >= 0.8*x[imx]) sec[1] <- n
>> 		if(x[2] >= 0.8*x[imx]) sec[2] <- 2
>> 	}else if(imx == n){
>> 		if(x[n - 1] >= 0.8*x[imx]) sec[1] <- n - 1
>> 		if(x[1] >= 0.8*x[imx]) sec[2] <- 1
>> 	}else{
>> 		if(x[imx - 1] >= 0.8*x[imx]) sec[1] <- imx - 1
>> 		if(x[imx + 1] >= 0.8*x[imx]) sec[2] <- imx + 1
>> 	}
>> 	sec <- sec[sec != 0]
>> 	c(imx, sec)
>> }
>>
>> apply(mat, 2, fun)
>>
>>
>> Note that the result comes with the maximum first and the others follow.
>>
>> Hope this helps,
>>
>>
>>
>> Em 24-05-2013 11:41, eliza botto escreveu:
>>> There you go!!!
>>>
>>> structure(c(0.706461987893674, 0.998391468394261, 0.72402995269242, 1.70874688194537, 1.93906363083693, 0.89540353128442, 0.328327645695443, 0.427434603701202, 0.591932250254601, 0.444627635494183, 1.44407704434405, 1.79150336746345, 0.94525563730664, 1.1025988539757, 0.944726401770203, 0.941068515436361, 1.50874009152312, 0.590015480056925, 0.311905493999476, 0.596771673581893, 1.01502499067153, 0.803273181849135, 1.6704085033648, 1.57021117646422, 0.492096635764151, 0.433332688044914, 0.521585941816778, 1.66472272302545, 2.61878329527404, 2.19154489521664, 0.493876245329722, 0.4915787202584, 0.889477365620806, 0.609135860199222, 0.739201878930367, 0.854663750519518, 0.948228727226247, 1.38569091844218, 0.910510759802679, 1.25991218521949, 0.993123416952421, 0.553640392997634, 0.357487763503204, 0.368328033777003, 0.344255688489322, 0.423679560916755, 1.32093576037521, 3.13420679229785, 2.06195904001605, 1.41493262330451, 1.35748791897328, 1.19490680241894, 0.70248875618332!
>>>    2, 0.338258418490199, 0.123398398622741, 0.138548982660226, 0.16170889185798, 0.414543218677095, 1.84629295875002, 2.24547399004563, 0.0849732189580101, 0.070591276171845, 0.0926010253161898, 0.362209761457517, 1.45769283057202, 3.16165004659667, 2.74903557756267, 1.94633472878995, 1.19319875840883, 0.533232612926756, 0.225531074123974, 0.122949089115578), .Dim = c(12L, 6L))
>>>
>>> Thanks once again..
>>> Elisa
>>>
>>>
>>>> Subject: Re: [R] Continuous columns of matrix
>>>> From: bhh at xs4all.nl
>>>> Date: Fri, 24 May 2013 12:36:47 +0200
>>>> CC: r-help at r-project.org
>>>> To: eliza_botto at hotmail.com
>>>>
>>>>
>>>> On 24-05-2013, at 12:24, eliza botto <eliza_botto at hotmail.com> wrote:
>>>>
>>>>> Dear useRs,If i have a matrix, say, 12 rows and 6 columns. The columns are continuous. I  want to find the index of maximum values and the actual maximum values. The maximum values in each column are the highest values and the values greater than or equal to 80% of the maximum value. Moreover, if a column has more than one maximum values than these values should come immediately next to each other.  For example, if you column 1 has a highest value in 6th row then the second maximum values cant be in row 5 or 7. And as the columns are continuous therefore, if maximum value is in row 12th, then the second maximum cant be in row 11 and 1.Thankyou very much indeed in advance
>>>>
>>>>
>>>> Incomprehensible.
>>>> What is a continuous column?
>>>>
>>>> Please give an example input matrix and and the result you want.
>>>>
>>>> Berend
>>>>
>>>>> Elisa
>>>>> 	[[alternative HTML version deleted]]
>>>>>
>>>>
>>>> Please post in plain text.
>>>>
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help