N.M
N.M

Reputation: 685

Perl PDL - getting the 80% lowest values in a vector

Is there an elegant PDL function which receives a list of values and returns a list of 80% of the original values which are the lowest?

For example: If I have a list like so: (9, 4, 1, 2, 7, 8, 3, 5, 6, 10)

I would like to get (1, 2, 3, 4, 5, 6, 7, 8) after calling this function on the original list (The order of the values does not matter - it does not need to sort the values).

I found PDL::Ufunc::oddpct which can return the 80'th percentile but I would like to get a list of the values up to that percentile. I can do it myself but if there's something out of the box - why not use it?

Thanks!!!

Upvotes: 4

Views: 473

Answers (3)

tsee
tsee

Reputation: 5072

Not PDL, but likely at least as fast:

Statistics::CaseResampling's select_kth function

Upvotes: 0

Bill Ruppert
Bill Ruppert

Reputation: 9026

Well, this is the "do it yourself" that you don't want to do, but it is so easy. PDL seems might heavy for this.

use strict;
use warnings;

my @list = (9, 4, 1, 2, 7, 8, 3, 5, 6, 10);

my @slist = sort {$a <=> $b} @list;
my @list80 = @slist[0..int(0.8*@slist)-1];

print "@list80";

Upvotes: 0

Borodin
Borodin

Reputation: 126742

I suggest you use pct to get the 80th percentile figure, combined with the where primitive to select everything beneath that value. Using your own data it looks like this

use strict;
use warnings;

use PDL;

my $list = pdl(9, 4, 1, 2, 7, 8, 3, 5, 6, 10);
print qsort $list->where($list <= $list->pct(80/100));

OUTPUT

[1 2 3 4 5 6 7 8]

Upvotes: 7

Related Questions