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 13:21:04 +0000 (UTC) Organization: Queen's University Lines: 40 Message-ID: <ipjmo0$b3r$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> <ipiqv9$cja$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-01-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1304256064 11387 172.30.248.46 (1 May 2011 13:21:04 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sun, 1 May 2011 13:21:04 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2643899 Xref: news.mathworks.com comp.soft-sys.matlab:724670 "Roger Stafford" wrote in message <ipiqv9$cja$1@fred.mathworks.com>... > "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 Sorry for my uncompleted explanation. My problem may be similar to a practical issue: taking time-domain sampling for instance, my sampling machine can sample wave magnitude data in a default time series a=[1,2,3,4,5,...] s. But this time series would miss some important peak values and broken lines in the sampled wavelet, which is found locating at b=[1.9, 2,1, 3.2, ...] s. So, I prefer to replace some sampling time series points in 'a' with the peak value time points in 'b', without changing the size of 'a' or repeating any elements in the series. Once I know the indices and values to be changed in 'a', I can update the time series of sampling operations easily. In this way, the sampled wave will have a much more analogical shape to what it should be. I think your previous discussion on the original problem will be helpful, except for some small changes... Should I start a new thread? Xiaodong