savita:

As others have noted, you're talking about a rather large dataset. I'd suggest not even bothering to reflect the data around the diagonal, and instead manipulate your indices to fetch the data:

$ cat pm_1198523.pl #!env perl use strict; use warnings; my $rData = [ [ 1, 3, 5, 7, 9 ], [ 0, 2, 4, 6, 8 ], [ 0, 0, 3, 6, 9 ], [ 0, 0, 0, 8, 4 ], [ 0, 0, 0, 0, 3 ] ]; bless $rData, 'foo'; print $rData->fetch(1,4), "\n"; print $rData->fetch(4,1), "\n"; print $rData->fetch(3,1), "\n"; print $rData->fetch(1,3), "\n"; package foo; sub fetch { my ($self, $r, $c) = @_; ($r, $c) = ($c, $r) if $c < $r; return $self->[$r][$c]; } $ perl pm_1198523.pl 8 8 6 6

Also, if you're building the file containing the data yourself, I'd suggest making all your data elements the same size so you can access the data directly from the file via seek.

If you'd like to reduce the size of your data file, and you're building it with fixed length elements, then you don't even need to store the lower "NA" triangle: instead you can use the idea above to fetch the data you're wanting by swapping the indices, and compute the seek address directly to compress out the lower triangle, like this:

$ cat pm_1198523_2.pl #!env perl use strict; use warnings; my ($R, $C, $sz); # Row, Column and element size $sz = 5; for my $r ([0,0], [1,0], [1,1], [2,0], [2,1], [2,2], [12,5]) { ($R,$C) = @$r; my $seek_addr = seek_addr($R, $C, $sz); print "[$R,$C] => $seek_addr\n"; } sub seek_addr { my ($R, $C, $sz) = @_; ($R,$C) = ($C,$R) if $R < $C; my $slot = $R*($R+1)/2 + $C; return $sz * $slot; } $ perl pm_1198523_2.pl [0,0] => 0 [1,0] => 5 [1,1] => 10 [2,0] => 15 [2,1] => 20 [2,2] => 25 [12,5] => 415

Note: You may want to adjust things depending on how you plan to use the data. Random access of the items is likely to be excruciatingly slow as other monks have mentioned. If you can arrange your algorithm so that you primarily iterate through columns or rows then you can make the disk cache work with you rather than against you. (You may have to swap R/C above depending on your access pattern.)

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: upper or lower triangular matrix to full by roboticus
in thread upper or lower triangular matrix to full by savita

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.