I want to count the occurrences of two float values at the same time.
my @Key_Str = swcols "%0.2f_%0.2f", $a_datas, $b_datas;
my %counts;
foreach my $key_str (@Key_Str) {
$counts{$key_str}++;
}
my (@p_a, @p_b @cnts);
foreach (sort keys %counts){
my ($z, $h) = split /_/, $_;
push @p_a, $z; push @p_b, $h; push @cnts, $counts{$_};
}
my $a= pdl(@p_a); my $b = pdl(@p_b); my $count = pdl(@cnts);
Is it possible same thing with pure PDL?
It's taking time using hash and array.
In more detail,
#!/usr/bin/perl
use PDL;
use Data::Dumper;
use feature 'say';
my $a_data = pdl(1.5, 2.1, 2.1, 1.5, -5.7);
my $b_data = pdl(12.2, 22.3, 22.3, 12.2, 15.3);
my @Key_Str = swcols "%0.2f_%0.2f", $a_data, $b_data;
my %counts;
foreach my $key_str (@Key_Str) {
$counts{$key_str}++;
}
my (@p_a, @p_b, @cnts);
foreach (sort keys %counts){
my ($z, $h) = split /_/, $_;
push @p_a, $z; push @p_b, $h; push @cnts, $counts{$_};
}
my $a= pdl(@p_a); my $b = pdl(@p_b); my $count = pdl(@cnts);
say $a;
say $b;
say $count;
result is
[-5.7 1.5 2.1]
[15.3 12.2 22.3]
[1 2 2]
I am not sure how to get the counts of the unique elements with a simple PDL function, but you can do it in a loop. For example:
Output: