Hi jnarayan81,

As pme and choroba mentioned, you will get more/better help if you describe the objective of your nearest function.

I did not closely inspect your code, but I took a guess at what you were trying to do and came up with the following example.

#!/usr/bin/env perl use strict; use warnings; use Data::Table; # Create a Data::Table with headers (assuming data is tab-delimited) my $dt = Data::Table::fromTSV( 'mat.txt', 1 ); # Get the number of rows in the Data::Table my $n_rows = $dt->nofRow; my $query = [ 37,35,59,70 ]; my $nearest_name = ''; my $min_dist; foreach my $i (0..$n_rows - 1){ my $row_ref = $dt->rowRef($i); # Get row of Data::Table as an ARRA +Y REF my $name = shift @{$row_ref}; # The name is in the first column my $dist = dist($query, $row_ref); $min_dist = !defined($min_dist) ? $dist : $dist < $min_dist ? $dist : $min_dist; $nearest_name = $dist <= $min_dist ? $name : $nearest_name; } print "The nearest to: "; print join(", ", @{$query}); print " is: $nearest_name\n"; exit; # Calculate the Euclidean distance between two vectors sub dist { my ($x, $y) = @_; unless(ref($x) eq 'ARRAY' and ref($y) eq 'ARRAY'){ die "Vectors must be given as array references" } unless (scalar @{$x} == scalar @{$y}) { die "Vectors are not of equal length"; } my $sum_sq = 0; my $len = scalar @{$x}; foreach my $i (0..$len - 1) { $sum_sq += ($x->[$i] - $y->[$i])**2; } return sqrt($sum_sq); }

I like the Data::Table module for manipulating tabular data, but there are many other ways to load/manipulate your data. If your definition of distance or best match is not euclidean distance then modify the dist subroutine accordingly.

UPDATE: Originally the dist sub returned $sum_sq. I changed it to return the correct euclidean distance which is sqrt($sum_sq).


In reply to Re: How to extract the best match from matrix by kevbot
in thread How to extract the best match from matrix by jnarayan81

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.