my ($matrix, $limits) = @_; # I get my data and limits from the arguments of subroutine my $j = 1; my ($pdl1, $pdl2, $n1, $n2); my ($angle, $dotproduct, $d, %angles); $pdl1 = pdl (@{$matrix}[0]); $n1 = norm $pdl1; while ($j<=$#{$matrix}) { $pdl2 = pdl (@{$matrix}[$j]); $n2 = norm $pdl2; $dotproduct = inner ($n1, $n2); $d = $dotproduct->sclr(); $angle = (180/3.14159)*acos($d); $angles{$j} = $angle; $j++; } my @sorted = sort {$angles{$a} <=> $angles{$b}} keys %angles;