0
$\begingroup$

I have a question, about verifying the value which are near to each other in such row, Is there any algorithm which can determine that ?

Suppose I have a vector $z_i =$ {$z_1 , z_2, z_3, . . . ., z_n$}, I want first to determine the maximum and minimum values of this vector, then check if the distance between each other value is smaller compare then maximum and to minimum values. In other words, it means dividing the vector into two categories and then check if the value is nearer to first category or second.

Is there any algorithm can do that?

thank you

$\endgroup$
1
$\begingroup$

Just a little bit of python:

import numpy as np

def splitVector(v):
    thres = (np.max(v)-np.min(v))/2
    return v[v<=thres], v[v>thres]

This will yield two vectors with just the lower or higher elements. If you want to preserve overall size and position of arguments, just a little modification will do it:

import numpy as np

def splitVector(v):
    thres = (np.max(v)-np.min(v))/2
    return np.where(v<=thres,v,0), np.where(v>thres,v,0)

This will fill in zeros for all elements not fulfilling the condition.

$\endgroup$
  • $\begingroup$ Thank you so much for your quick answer. Yes, but what is the name of algorithm you are using? I mean is there known algorithm I can use in that case? $\endgroup$ – Gze Apr 16 at 7:33
  • $\begingroup$ This is so simple, it's not really an algorithm. If you want a name, I guess "categorizing by thresholding" would come close to what is happening there. $\endgroup$ – Max Apr 16 at 7:50
  • 1
    $\begingroup$ @Max ,, Do you know about k-mean algorithm? I think it will be suitable in that case. I'm not sure because I don't know about it so much. $\endgroup$ – Zeyad_Zeyad Apr 16 at 8:09
  • 1
    $\begingroup$ Yes, I know this algorithm. It's a little bit different from what Gze asked, as it will minimize the distance to the cluster points (in this case min and max) for the sum of distances, while what I proposed will do it element wise. It may well be suitable, that depends on what Gze actually wants to do. $\endgroup$ – Max Apr 16 at 8:32
  • $\begingroup$ Thanks to both of you .. As I checked, we can do it either by K-mean algorithm OR that method provided above. . Thanks again $\endgroup$ – Gze Apr 16 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.