Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found