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

Re: Meditations on the Nature of Code Exams

by jeroenes (Priest)
on Mar 21, 2001 at 18:06 UTC ( #66022=note: print w/ replies, xml ) Need Help??


in reply to Meditations on the Nature of Code Exams

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)


Comment on Re: Meditations on the Nature of Code Exams
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://66022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (17)
As of 2014-08-27 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls