Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Extracting info from matrix

by Angharad (Pilgrim)
on Aug 10, 2005 at 18:52 UTC ( [id://482715]=perlquestion: print w/replies, xml ) Need Help??

Angharad has asked for the wisdom of the Perl Monks concerning the following question:

Right now I'm playing around with matrices and PDL. I have some code that will do SVD on a matrix ($a, $b, $c) = svd($matrix), the results for $b looking like this
[0.97069051 0.57504129 0.09337227 0.012809204]
What I am now trying to do is to extract the three values from matrix b and assign them as three different variables. My first thought was to copy the contents of the matrix into an array and then use substr. This works up to a point but the finished program will be dealing with a number of matrices for which svd will have to be performed, so a general substr approach wont work (as the number of decimal places will vary with each calculation). I'm guessing that I might be able to extract the data using regular expressions instead? Any suggestions much appreciated

Replies are listed 'Best First'.
Re: Extracting info from matrix
by injunjoel (Priest) on Aug 10, 2005 at 20:25 UTC
    Greetings all,
    Though a little more code to illustrate what you have tried up to now might be helpful my intuitive feeling is that $b is a row vector and could therefore be addressed as such. No need for splitting and regexp. Im not sure if you are using PDL::Matrix or PDL::MatrixOps but I know there is an issue with element addressing being either (row, column) or (column, row). I would suggest reading up on PDL::Indexing before rolling out a regexp solution for this, slice should suffice.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: Extracting info from matrix
by shemp (Deacon) on Aug 10, 2005 at 19:18 UTC
    If your value of $b always looks like your example, in that its a sequence of numbers with whitespace in between, you could just split them on whitespace. (Im not sure if the square brackets are actually part of the string or not)

    Anyway, something like this:

    # if the brackets are actually there: $b =~ s/\[|\]//g; my @values = split /\s+/, $b;

    I use the most powerful debugger available: print!
Re: Extracting info from matrix
by tall_man (Parson) on Aug 10, 2005 at 19:24 UTC
    Why not do:
    ($a, $b, $c) = svd($matrix); # convert to a perl list my @bvals = list $b;
    There's no need to parse out the values from a string. (By the way, your example has four values, not three).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://482715]
Approved by sk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found