viored has asked for the wisdom of the Perl Monks concerning the following question:
Hey,
So I'm trying to make a lower triangular distance matrix in an itterative fashion. I had a function hd(a,b) which returns the distance between two objects a and b. I then want to iteratively add rows to this matrix as more objects are added, but I want to only add new rows, not recalculate the old ones. so I wrote this
for reference, main::M stores all the objects, and $main::hd is initialized as [];, and hd::save is initialized at 1 and updated each time the loops completes with hd::save = hd::size; Whenever I follow this up with a module, it says the the first row of my matrix has no columns. but, when I use the following code (something I wrote for myself for self-reference)$hd::size1 = scalar @{\@{$main::M[0]}}; $hd::size = $hd::size1 - 1; for ($hd::i=$hd::save; $hd::i<= $hd::size; $hd::i++) #this creates + and adds to the hd array { $hd::store= []; for ($hd::j=0; $hd::j < $hd::i; $hd::j++) { $hd::temp = &hd($main::M[0][$hd::i], $main::M[0][$hd::j]); print $hd::temp; print "\n"; push $hd::store, $hd::temp; } push @{$main::hd}, $hd::store; } $hd::save = $hd::size
The module works perfectly. I think the error is how I'm iteratively storing things. Any ideas? EDIT: The error was in my definition of hd::save. I orginally defined it as $hd::size, but that causes it to recalculate the last object from the previous time step. Changing it to $hd::save=$hd::size +1; fixed everything. This meant the matrix was not diagonal, it had weird kinks in it. Thanks everyone who helped!!!!!#!/usr/bin/perl #ex2 use warnings; use strict; sub clust; $main::hd=[[]]; $main::a = [0.25]; $main::b = [0.25,0.5]; $main::C = [1,0.75,0.75]; $main::hdt = 0.4; $main::c = []; $main::c1 = 1; $main::c2 = 0.75; $main::c3 = 0.75; #how to add elements to an array push $main::c, $main::c1; push $main::c, $main::c2; push $main::c, $main::c3; #how to add arrays to arrays push @{$main::hd}, $main::a; push @{$main::hd}, $main::b; push @{$main::hd}, $main::c; &clust; for ($loop::i=0; $loop::i<=3; $loop::i++) { print $clust::cluster_ids->[$loop::i], "\n"; } sub clust { use Algorithm::Cluster::Thresh; use Algorithm::Cluster qw/treecluster/; $clust::tree = treecluster(data=>$main::hd, method=>'a'); $clust::cluster_ids = $clust::tree->cutthresh($main::hdt); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Array storage issue
by hippo (Bishop) on Apr 06, 2014 at 21:44 UTC | |
by LanX (Saint) on Apr 06, 2014 at 21:52 UTC | |
by viored (Novice) on Apr 06, 2014 at 22:04 UTC | |
by GrandFather (Saint) on Apr 06, 2014 at 22:46 UTC | |
by Anonymous Monk on Apr 06, 2014 at 22:23 UTC | |
by viored (Novice) on Apr 06, 2014 at 22:01 UTC | |
by AnomalousMonk (Archbishop) on Apr 06, 2014 at 22:25 UTC | |
by Anonymous Monk on Apr 06, 2014 at 22:32 UTC | |
by viored (Novice) on Apr 06, 2014 at 22:17 UTC | |
by AnomalousMonk (Archbishop) on Apr 06, 2014 at 22:52 UTC | |
by LanX (Saint) on Apr 06, 2014 at 23:09 UTC | |
by AnomalousMonk (Archbishop) on Apr 06, 2014 at 23:18 UTC | |
| |
by LanX (Saint) on Apr 06, 2014 at 22:29 UTC | |
Re: Array storage issue
by Anonymous Monk on Apr 06, 2014 at 22:52 UTC | |
by viored (Novice) on Apr 07, 2014 at 06:01 UTC |
Back to
Seekers of Perl Wisdom