Hi Monks
I've already posted once on this topic today. I have gotten a little further but alas have got somewhat stuck. Basically, I have a dataset in the form of a matrix and I want to perform SVD on this dataset and then reconstruct the matrix with the most signficant data. I already have the code in the form of a Matlab M script so have been trying to convert it into perl using the PDL module. Here is the perl script so far (please bear in mind that I'm using a test dataset here, the real one will be much bigger).
#!/usr/bin/perl
use PDL;
my $a = pdl [
[ 0.6490, 0.0330, 0.1670, 0.3700],
[ 0.1150, 0.0290, 0.0480, 0.0650],
[ 0.0070, 0.3570, 0.3870, 0.2010],
[ 0.0630, 0.0760, 0.0540, 0.0560],
[ 0, 0.0400, 0.0160, 0.0160],
[ 0.0120, 0.0620, 0.0430, 0.0380],
[ 0.1530, 0.4020, 0.2850, 0.2550],
];
($U, $S, $V) = svd($a);
# create square matrix for 'S'
@valuesS = list $S;
$SqS = pdl [
[ $valuesS[0], 0, 0, 0],
[ 0, $valuesS[1], 0, 0],
[ 0, 0, $valuesS[2], 0],
[ 0, 0, 0, $valuesS[3]],
];
# transpose V
$Vt = $V->transpose();
$Us = $U->slice('0:1,0:6');
$Ss = $SqS->slice('0:1,0:1');
$Vs = $Vt->slice('0:3,0:1');
print "$Us\n";
print "$Ss\n";
print "$Vs\n";
$recA = $Us * $Ss * $Vs;
print "$recA\n";
Most of this works just fine, but not this line
$recA = $Us * $Ss * $Vs;
Where I get the following error
PDL: PDL::Ops::mult(a,b,c): Parameter 'b'
PDL barfed: Mismatched implicit thread dimension 1: should be 7, is 2
I *think* this is due to the fact that the three matrixes I am trying to multiply are of differeing dimensions. I am trying to emulate the following matlab command
recA = U(:,1:ns)*S(1:ns,1:ns)*Vt(1:ns,:)
Any suggestions as to a work around appreciated folks :)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.