Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: find nearest,closest value Date: Sun, 1 May 2011 05:27:05 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 29 Message-ID: <ipiqv9$cja$1@fred.mathworks.com> References: <gmf901$45e$1@fred.mathworks.com> <gnaitl$5dn$1@fred.mathworks.com> <i8o734$a2d$1@fred.mathworks.com> <i8ofdc$ove$1@fred.mathworks.com> <i8p8t2$p4c$1@fred.mathworks.com> <i8q3g8$o52$1@fred.mathworks.com> <i8qds3$ana$1@fred.mathworks.com> <i8qhm1$cfp$1@fred.mathworks.com> <i8s0rb$la7$1@fred.mathworks.com> <i8s483$i4a$1@fred.mathworks.com> <ipiea9$3vr$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-03-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1304227625 12906 172.30.248.48 (1 May 2011 05:27:05 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sun, 1 May 2011 05:27:05 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 1187260 Xref: news.mathworks.com comp.soft-sys.matlab:724638 "Xiaodong" wrote in message <ipiea9$3vr$1@fred.mathworks.com>... > This is an interesting problem, similar to my present problem: > Back to the original problem, yet, I do not only attempt to find the closest value from a to b, but also attempt to replace the closest value in a to that in b, and return the index of being replaced elements in a. Notice that if two elements in b corresponds to the same element in a as the common nearest value, then I may want to replace that value in a with the closest or most lefthandside nearest points in b. > > For example, > a=[1, 2, 3, 4, 5, 6]; > b=[2.1, 1.9, 3.2]; > Obviously, both 1.9 and 2.2 in b close to 2 in a, but I want to replace 2 with 1.9 as ,1.9 is the nearest point in the leftside of 2. Such that, I attempt to obtain the following new vector and index of elements being replaced: > new_a=[1, 1.9, 3.2, 4, 5, 6]; > index_replaced_in_a=[2, 2, 3]; > > Could you guys figure out a way to do so in a general way? a,b can be vectors or arrays of coordinates set (not so important)... > > Thank you very much! > > Cheers, > Xiaodong - - - - - - - - - To replace elements of vector 'a' by those closest in 'b' in the problem of the original posting (5 Feb 2009) in this thread, just do: a = b(ib); using the 'ib' index derived in the code of 16 Feb 2009 (as corrected by Shaun) and I believe the index in 'ib' would be the index vector you speak of. However, this is not in agreement with your example where the above code would produce ib = [2,2,3,3,3,3] and new_a = [1.9,1.9,3.2,3.2,3.2,3.2]. Can you tell me by what logic you don't have all the elements of 'a' replaced by their closest elements of 'b'? You may be thinking of an altogether different problem but I don't quite know what that would be unless you are thinking only of values of 'a' that are bracketed on both sides by values in 'b'. At the moment I cannot say for sure how this two-year-old code chooses between two equally distant values in 'b'. I would have to study that code carefully. I have reason to believe it selects the first encountered, moving left-to-right, in 'b' among equal candidates, though in choosing between equally distant upper and lower values in 'b' it may give preference to the lower values. Roger Stafford