G'day azheid,

"I have just enough coding experience to hang myself with a short rope. ... I tried to create a second similar matrix that stored the minimum distance to each node, but I am having trouble with correctly referencing the distance matrix."

I think you need some basic foundation knowledge.

In both of these lines, you're assigning an arrayref to an array:

my @distance_mat=[]; ... my @vit_dist=[ [ 0] ];

And in this line:

${$vit_dist[$i]}[$j]=min(${$vit_dist[$i-1]}[$j]+${$distance_mat[$i]}[$ +j];

you appear to be calling a min() function. You've failed to include a closing parenthesis so that leaves me guessing what is intended (and, potentially, what else has been left out or accidentally included). Perl has no min() function, Algorithm::Viterbi's documentation shows no min() function and you show no code for a min() function: I'm out of guesses.

Attempting to access a multi-dimensional array element with code like:

${${$distance_mat[$i]}[$j]}[$k]

is difficult to read and maintain: and, therefore, error-prone.

I suggest starting with "Perl references short introduction" and "Perl data structures intro".

Here's how I might have written the first piece of code you posted. I've reduced the number of elements from 10 to 2 for demonstration purposes. This should give you an idea of how you can (much simply) drill down into this sort of data structure and access individual elements.

#!/usr/bin/env perl use strict; use warnings; my $max_index = 1; my $matrix; for my $i (0 .. $max_index) { for my $j (0 .. $max_index) { for my $k (0 .. $max_index) { $matrix->[$i][$j][$k] = rand 1; } } } use Data::Dump; dd $matrix;

Output:

[ [ [0.290579816865833, 0.491432556740136], [0.888187692306143, 0.761365009561764], ], [ [0.865674078359991, 0.284696276285089], [0.0562126863718184, 0.781856824452085], ], ]

-- Ken


In reply to Re: Viterbi application by kcott
in thread Viterbi application by azheid

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.