in reply to for-loop issue - swap gets crazy large
I'm fairly sure (untested) that this will produce the same results in @group whilst using minimal memory and much more quickly:
#! perl -slw use strict; use List::Util qw[ sum ]; sub distance { my( $iref, $wref, $dim, $s, $u ) = @_; return sqrt( sum( map{ ( $iref->[ $_ ][ $u ] - $wref->[ $_ ][ $s ] ) **2 } 0 .. $dim - 1 ) ) / $dim; } my $U = my $S = 100000; my $dimMax = 5; my @group; my @input_train = ...; my @weights = ...; for my $u ( 0 .. $U - 1 ) { my $minDistance = 100; $group[$u] = -99; my $minS; for my $s ( 0 .. $S -1 ) { my $dist = distance( \@input_train, \@weights, $dimMax, $s, $u + ); if( $dist < $minDistance ) { $minDistance = $distance[ $u ][ $s ]; $minS = $s; } } $group[ $u ] = $minS; }
|
|---|