Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm responding merely on your ranting about Matlab. First a few statements:

Try to think like:

The right tool for the job.

Matlab is different, but not bad because it is different.

Matlab is a Matrix oriented programming language, where perl is more or less item oriented (but see PDL).


I have been using perl and matlab on a daily basis for a couple of years, but only recently I ran into a situation that forced me to redo a matlab-solution in perl (or C, but for obvious reasons I choose perl). BTW, I studied chemistry myself.

What struck me, is that some things can be written very smoothly in the perl way, but that some other things get very complicated when you have to release the matrix approach. It's similar like rewriting some complicated SQL (with lot's of joins and where-statements on different columns from different tables) into perl.

For the sake of the argument, let's assume that we have some data in a 5x5 matrix. In Matlab it's called 'a', and in perl it's a 2D-array '$a'. Let's assume it's in a tab-delimited text file, than you would fetch it:

Matlab: load data.txt a=data; Perl: $a = supersplit_open( 'data.txt' ); #Without supersplit, it's more + code
So far so good. Let's say I want to summate the rows (a basic starting point for statistics):
Matlab: sum(a') Perl: $sum = map{ my $c = 0; map{ $c += $_; }@$_; $c; } @$a;
If I want to summate columns:
Matlab: sum(a) Perl: my $last = $#{$a->[0]}; $sum = [ map {0} 0..$last ]; map{ $sum->[$i] += $_->[$i] for my $i (0..$last) } @$a;
Maybe you have to see that it is easy to select rows/columns in matlab, and do some logic on it:
large=find( a(:,1) > 1E10 ); large_ok = find( a(:,1) > 1E10 & a(:,3) > 0 ); sum( a(large,:) ) mean( a(large_ok,:) )
I refuse to recode that in perl. I had to do it once, and it was really awful. But I can do it in SQL easily as well:
SELECT mean(a1), mean(a2), mean(a3), mean(a4), mean(a5) FROM a WHERE a1 > 1.0E10 and a3 > 0;
And these are the basic things I run into daily, in collecting and further analysing data.

But whenever you run into something that has to be done item-wise, Matlab solutions get uglier. For example, a running sum of an 1D-array 'b':

Matlab: for i=1:length(b) s(i) = sum(b(1:i)); end Perl: my $localsum = 0; $s = [ map{ $localsum += $_ } @$b ];
So, whenever you stay as close to the matrix as possible, you will see that much of your coding is quite easy in Matlab, depending on the actual assignment.

Cheers,

Jeroen
"We are not alone"(FZ)


In reply to Re: Meditations on the Nature of Code Exams by jeroenes
in thread Meditations on the Nature of Code Exams by Elgon

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (14)
    As of 2014-08-22 12:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (156 votes), past polls