Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Syntactic Confectionery Delight
 
PerlMonks  

Re: Employing loops and constructing a matrix

by NetWallah (Monsignor)
on Apr 24, 2012 at 05:07 UTC ( #966726=note: print w/ replies, xml ) Need Help??


in reply to Employing loops and constructing a matrix

Try to get your data into a convenient structure - i'd suggest an array-of-hashrefs.

Your results should be a 2-D array.

Here is some code to get you started (populated with sample data). You need to figure out how to get your data into this structure:

use strict; use warnings; my @points = ( {X=>79.620, Y=>66, Z=>12.3}, {X=>66.4, Y=>77, Z=>43.1}, {X=>73.6, Y=>55, Z=>93.7} ); my @results; for my $x(0..$#points){ my $p1 = $points[$x]; for my $y (0..$#points){ my $p2 = $points[$y]; my $dist = ( $p1->{X} - $p2->{X} ) **2 + ( $p1->{Y} - $p2->{Y} ) **2 + ( $p1->{Z} - $p2->{Z} ) **2 ; $results[$x][$y] = $dist; } } #------------------- for my $r (@results){ printf "%3.2f\t",$_ for @$r; print "\n"; }
Yes - there is room for optimization, since distances are symmetric, but, at this point, operational and simple code trumps efficient code that introduces complexity.

             All great truths begin as blasphemies.
                   ― George Bernard Shaw, writer, Nobel laureate (1856-1950)


Comment on Re: Employing loops and constructing a matrix
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966726]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2013-05-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (519 votes), past polls