Hi!
I have a problem of finding a specific value in a column. For example, I
have a matrix with say 2 columns
X Y
1 -2.0341602 9.036689e-05
2 -1.4287230 1.807338e-04
3 -1.1194402 2.711007e-04
4 -1.0327582 3.614676e-04
5 -0.8130556 4.518344e-04
6 -0.7138212 5.422013e-04
7 -0.6634425 6.325682e-04
8 -0.6512083 7.229351e-04
9 -0.6176286 8.133020e-04
10 -0.5897241 9.036689e-04
Now if I have some value of x=-0.6523. I need to find a value in the X
column that is the closest to my value of x then read off the row number
and then take the corresponding value in column Y. What I am not sure is
how to do the first search where I would search by decimal places and take
the smallest absolute distance between the numbers. For example if he finds
the first value which is correct in this case - and then 0 and then 6 and
then 5 but now there is no 2 for that specific decimal place so he would
calculate the distance between the one before and the one after and see
which one is smaller. For that which is smaller would be the final X value.
Can someone please give me a hint on how to proceed. Thanks.
maybe something like this could be of help:
mat <- matrix(rnorm(20), 10, 2)
val <- -0.6523
###################
ind <- which.min(abs(mat[, 1] - val))
mat
mat[ind, ]
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: <Ita.Cirovic-Donev at hypo-alpe-adria.com>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, February 13, 2006 1:30 PM
Subject: [R] search algorithm
>
>
>
>
> Hi!
>
> I have a problem of finding a specific value in a column. For
> example, I
> have a matrix with say 2 columns
>
> X Y
> 1 -2.0341602 9.036689e-05
> 2 -1.4287230 1.807338e-04
> 3 -1.1194402 2.711007e-04
> 4 -1.0327582 3.614676e-04
> 5 -0.8130556 4.518344e-04
> 6 -0.7138212 5.422013e-04
> 7 -0.6634425 6.325682e-04
> 8 -0.6512083 7.229351e-04
> 9 -0.6176286 8.133020e-04
> 10 -0.5897241 9.036689e-04
>
> Now if I have some value of x=-0.6523. I need to find a value in the
> X
> column that is the closest to my value of x then read off the row
> number
> and then take the corresponding value in column Y. What I am not
> sure is
> how to do the first search where I would search by decimal places
> and take
> the smallest absolute distance between the numbers. For example if
> he finds
> the first value which is correct in this case - and then 0 and then
> 6 and
> then 5 but now there is no 2 for that specific decimal place so he
> would
> calculate the distance between the one before and the one after and
> see
> which one is smaller. For that which is smaller would be the final X
> value.
> Can someone please give me a hint on how to proceed. Thanks.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi,
I'm not sure how much weight you're putting on efficiency for your
algorithm, but that sounds unnecessarily complicated. If your matrices
are not too big then maybe something like this would work. Let your
matrix be M and your value be a, then
yoursearch <- function(M, a) {
# generate abs dist vector
dist <- abs(M[,"X"]-a)
# which element is min dist
argmin <- which.min(dist)
# return corresponding Y value
M[argmin,"Y"]
}
There are so many ways to do this faster (google for search algorithms
if you feel so inclined), but I think simplicity is good unless you
have enormous matrices or need to do it a very large number of times.
Anyway, hope this helps
James
On Mon, Feb 13, 2006 at 01:30:21PM +0100, Ita.Cirovic-Donev at
hypo-alpe-adria.com wrote:> I have a problem of finding a specific value in a column. For example, I
> have a matrix with say 2 columns
>
> Now if I have some value of x=-0.6523. I need to find a value in the X
> column that is the closest to my value of x then read off the row number
> and then take the corresponding value in column Y. What I am not sure is
> how to do the first search where I would search by decimal places and take
> the smallest absolute distance between the numbers. For example if he finds
> the first value which is correct in this case - and then 0 and then 6 and
> then 5 but now there is no 2 for that specific decimal place so he would
> calculate the distance between the one before and the one after and see
> which one is smaller. For that which is smaller would be the final X value.
> Can someone please give me a hint on how to proceed. Thanks.
There is a function in the Hmisc package that will help. Example:> require(Hmisc) > x <- sort(runif(10)) > x[1] 0.1225542 0.1620869 0.2197772 0.3187375 0.5498879 0.5644445 0.5812717 0.7380532 0.8187384 0.9063713> whichClosest(x,.41)[1] 4> x[4][1] 0.3187375 Or for your matrix (yourmat), this should work (but I haven't tested it) yourmat[ whichClosest(yourmat[,'X'] , -0.6523) , 'Y'] -Don At 1:30 PM +0100 2/13/06, Ita.Cirovic-Donev at hypo-alpe-adria.com wrote:>Hi! > >I have a problem of finding a specific value in a column. For example, I >have a matrix with say 2 columns > > X Y >1 -2.0341602 9.036689e-05 >2 -1.4287230 1.807338e-04 >3 -1.1194402 2.711007e-04 >4 -1.0327582 3.614676e-04 >5 -0.8130556 4.518344e-04 >6 -0.7138212 5.422013e-04 >7 -0.6634425 6.325682e-04 >8 -0.6512083 7.229351e-04 >9 -0.6176286 8.133020e-04 >10 -0.5897241 9.036689e-04 > >Now if I have some value of x=-0.6523. I need to find a value in the X >column that is the closest to my value of x then read off the row number >and then take the corresponding value in column Y. What I am not sure is >how to do the first search where I would search by decimal places and take >the smallest absolute distance between the numbers. For example if he finds >the first value which is correct in this case - and then 0 and then 6 and >then 5 but now there is no 2 for that specific decimal place so he would >calculate the distance between the one before and the one after and see >which one is smaller. For that which is smaller would be the final X value. >Can someone please give me a hint on how to proceed. Thanks. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Hello, the dimension of the matrix is [3565x18]. what would be your suggestion which method to use? Ita
Ita.Cirovic-Donev at hypo-alpe-adria.com wrote:> > > > Hello, > > the dimension of the matrix is [3565x18]. what would be your suggestion > which method to use?I think we should apply linear regression on a 3565x18 matrix, but then let us think about what the underlying question could have been. Do you want to use the search algorithm to look for this question? Otherwise, if the matrix would have been 1x1 containg the number 42 .... ;-) Uwe Ligges> Ita > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html