Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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.


"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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (9)
    As of 2018-06-21 11:09 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (118 votes). Check out past polls.