1

For instance for N highest numbers, lets say N = 3

I have a and want to get b

 a = np.array([12.3,15.4,1,13.3,16.5])
 b = ([15.4,13.3,16.5])

Thanks in advance.

Jan
  • 45
  • 7

1 Answers1

1

well, my take on this:

  1. Make a copy of the original array;
  2. Sort the copied array to find the n highest numbers;
  3. Go through the original array and after comparing its numbers to n highest numbers from the previous step move needed ones in a resulting array.

var a = [12.3,15.4,1,13.3,16.5], n = 3, x = 0, c =[]; // c - the resulting array
var b = a.slice(); // copy the original array to sort it 
for(var i = 1; i < b.length; i++) { // insertion sorting of the copy
  var temp = b[i];
  for(var j = i - 1; j >= 0 && temp > b[j]; j--) b[j + 1] = b[j];
  b[j + 1] = temp;
}
for(var i = 0; i < a.length; i++) { // creating the resulting array
  for(var j = 0; j < n; j++) {
    if(a[i] === b[j]) { 
      c[x] = a[i]; x++; // or just c.push(a[i]);
    }
  }
}
console.log(c);

The example is written in Javascript and is somewhat straightforward, but, in fact, it is quite language agnostic and does the job.

curveball
  • 4,320
  • 15
  • 39
  • 49