Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Convert matrix formats

by naturalsciences (Beadle)
on Oct 31, 2010 at 14:43 UTC ( #868590=note: print w/ replies, xml ) Need Help??


in reply to Re: Convert matrix formats
in thread Convert matrix formats

No :D no padding with zeroes. I want to make the upper half a symmetrical one to the lower.


Comment on Re^2: Convert matrix formats
Replies are listed 'Best First'.
Re^3: Convert matrix formats
by BrowserUk (Pope) on Oct 31, 2010 at 15:10 UTC
    I want to make the upper half a symmetrical one to the lower.

    Ah! Thought it was too easy. That'll take 2 lines :)

    #! perl -slw use strict; use Data::Dump qw[ pp ]; sub fillLTM { my @lt; my $n = 1; push @lt, [ splice @_, 0, $n++ ] while @_; return @lt; } ## read raw data while( my $rawData = <DATA> ) { ## Populate LT matrix my @lt = fillLTM( split ' ', $rawData ); pp \@lt;; for my $y ( 0 .. $#lt -1 ) { push @{ $lt[$y] }, $lt[$_][$y] for $y+1 .. $#{ $lt[-1] }; } pp \@lt;; } __DATA__ 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    Produces:

    C:\test>junk [ [1], [2, 3], [4, 5, 6], [7, 8, 9, 10] ] [ [1, 2, 4, 7], [2, 3, 5, 8], [4, 5, 6, 9], [7, 8, 9, 10] ] [ [ 1], [ 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10], [11, 12, 13, 14, 15] ] [ [ 1, 2, 4, 7, 11], [ 2, 3, 5, 8, 12], [ 4, 5, 6, 9, 13], [ 7, 8, 9, 10, 14], [11, 12, 13, 14, 15] ]

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I like your sub there :) Not relevant to my interests but its pretty and understandable (even to a newb like me). What is relevant to my interests is the actual converting part.

      for my $y ( 0 .. $#lt -1 ) { push @{ $lt$y }, $lt$_$y for $y+1 .. $#{ $lt-1 };

      I have some difficulties of understanding it clearly. Can you comment on this by which logic the first for conditions are set ( 0 .. $#lt -1 ) also the combination $# I have not encountered something like this what does it mean?
        Can you comment on this by which logic the first for conditions are set ( 0 .. $#lt -1 ) also the combination $# I have not encountered something like this what does it mean?

        Absolutely. That's what this place is all about.

        [ [1], [2, 3], [4, 5, 6], [7, 8, 9, 10] ]
        ### $#array is the highest index in @array. my $yMax = $#lt; ## $#{ ... } is the highest index in the array referenced by ... ## $array[ -1 ] is the last element in the @array ## So the following sets $xMax to highest index of the array (referenc +e) ## in the last element of @lt. Ie. The longest subarray. my $xMax = $#{ $lt[-1] }; ## Iterate through each the subarrays except the last which does need +padding for my $y ( 0 .. $yMax -1 ) { ## for $lt[ $y=0 ] ([1]), we need to add elements $y+1..$xMax ( +1..3) ## for $lt[ $y=1 ] ([2,3]), we need to add elements $y+1..$yMax ( +2..3) ## And the value we add at $lt[ $y ][ $x ] comes from $lt[ $x ][ $ +y ]. for my $x ( $y+1 .. $xMax ) push @{ $lt[ $y ] }, $lt[ $x ][ $y ]; } }

        BTW: please use <code></code> tags when posting code snippets. That way they are readable :)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (17)
As of 2015-07-31 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls