in reply to Convert matrix formats
Or is it something that could be easily done without the use of modules?
I'm deeply suspicious of questions that seem too easy, but ...
It kinda depends what form you have or obtain the lower-triangle matrix in to start.
But assuming you've loaded the LT matrix into a 2d array, then "converting to a square matrix" consists of padding the upper triangle with zeros, which is one line of perl:
#! 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;; ## Pad to square matrix push @{ $lt[$_] }, (0)x( @{ $lt[-1] } - @{ $lt[$_] } ) for 0 .. $# +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, 0, 0, 0], [2, 3, 0, 0], [4, 5, 6, 0], [7, 8, 9, 10] ] [ [1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15] ] [ [1, 0, 0, 0, 0], [2, 3, 0, 0, 0], [4, 5, 6, 0, 0], [7, 8, 9, 10, 0], [11, 12, 13, 14, 15] ]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Convert matrix formats
by naturalsciences (Beadle) on Oct 31, 2010 at 14:43 UTC | |
by BrowserUk (Patriarch) on Oct 31, 2010 at 15:10 UTC | |
by naturalsciences (Beadle) on Nov 01, 2010 at 15:51 UTC | |
by BrowserUk (Patriarch) on Nov 01, 2010 at 16:13 UTC | |
by naturalsciences (Beadle) on Nov 02, 2010 at 15:53 UTC |