The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 nearest neighbours algorithm
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

kubra

Slovakia
10 Posts

Posted - 11/02/2015 :  09:16:45 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 8/8.5
Operating System:Win7

Hello

I have an array of many data points (it depends on measurment, but around few thousands). These data points are not regularly distributed in array, because of measurment errors. However, i need to go through measured points (one by one), find their nearest neighbours and store x and y values in vector. Number of nearest neighbours is given by constant (between 25 and 121, from SF value given by (2*SF+1)^2). I know, these numbers are huge, and i am ok with long duration of calculating.
My algorithm does that. However, it is written to find at least so many nearest neighbours, as the constant is set. So, if constant is 25, for various points it finds 27, 28, 30 etc. points, and stops, when number of them exceed value of constant. If it is less, than value of constant, it erases all values in vector, rises up array, in which it is searching for NN, and does that again. Therefore calculation time (which is by itself long enough) is extending.
My code:
for(C=1; C<=nmbCols-1; C++){
for(R=0; R<=nmbRows-1; R++){
if(!is_missing_value(wks.Cell(R,C))){
i=SF;
j=SF;
do{
for(rR=R-j; rR<=R+j; rR++){
if(rR<0 || rR > nmbRows-1){
;
}
if(rR>=0){
for(cC=C-i; cC<=C+i; cC++){
if(cC<=0 || cC > nmbCols-1){
;
}
if(cC>0){
if(!is_missing_value(wks.Cell(rR,cC))){
vecH.Add(wks.Cell(rR, 0));
vecM.Add(wks.Cell(rR, cC));
}
if(is_missing_value(wks.Cell(rR,cC))){
;
}
if(cC==C+i){
break;
}
}
}
}
if(rR==R+j){
break;
}
}
if((vecM.GetSize())<((2*SF+1)^2)){
i++;
j++;
vecH.RemoveAll();
vecM.RemoveAll();
}
if((vecM.GetSize())>=((2*SF+1)^2)){
;
}
}while((vecM.GetSize())<((2*SF+1)^2));

After code is finished, program does euclidian distances calculations from the calculated data point, and sorts them in vector, and by that it chooses desired number of NN.
I know, it is very specific problem. Maybe if somebody has already done NN algorithm, or there is Origin C command i haven´t found...or has an idea for such edit of code, so it would not have to do repeating calculations... I would really appreciate it :)

Edited by - kubra on 11/02/2015 09:23:17 AM

SeanMao

China
288 Posts

Posted - 11/03/2015 :  05:28:10 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

We have a built-in X-function levelcrossing that is supported since Origin 8.1 that may help you find the NN X coordinates at a given level. You can find the reference from the link below:

http://www.originlab.com/doc/X-Function/ref/levelcrossing

Give it a try and see if could be some help.

Regards!

Sean

OriginLab Tech. Service
Go to Top of Page

kubra

Slovakia
10 Posts

Posted - 11/03/2015 :  1:10:18 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hello Sean

Thank you for reply. However, data in my dataset are more-less randomly distributed, and i can´t say beforehand how big is the region, where NN will be. Also, minimum required version is 8.1 SR0 (8.6, eventually), and i need to run my program on version 8.

But thanks again for reply!
Have a nice day
Branislav
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000