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
$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
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)
#!/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); }
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!!!!!

In reply to Array storage issue by viored

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.