Doug's MATLAB Video Tutorials
August 18th, 2010
MATLABdo: The Way of MATLAB
I work in technical support, so very often I get strange requests.
Really strange.
My first question is “Why do you want to do that?” The reason I ask this is very often new MATLAB users are fighting against the way that MATLAB wants to work. If Aikido is “the Way of harmonious spirit” (or “the Way of Water” as I learned it) then MATLABdo should be “the Way of MATLAB”.
Once users understand the MATLAB way of doing things, they can often do a task in a better way and then the specific syntax question they had does not matter. In fact, their code often becomes simpler, shorter and easier to maintain.
What is The MATLAB Way?
It is really hard to say, but I know it when I see someone going against it.
Let’s look at an example of going against MATLAB and how to fix it.
This MATLAB user wanted to make a series of variables to store some scalars. For example:
a1 = 11;
a2 = 22;
a3 = 33;
This lead to the next problem, where they would want to refer to one of these variables, but would not know which until run time. They just knew they would want to use one with a value of i.
i = 3;
So, they would want the following when i is three:
answer = a3 + i;
But, they would want the following when i is one:
answer = a1 + i;
That is when they came to ask me how they could do this.
The answer would be:
answer = eval(['a' num2str(i) ' + ' num2str(i) ';']);
However, to answer this question would be a great disservice to this MATLAB user, because their code should look like this:
a = [11 22 33];
i = 3;
answer = a(i) + i;
Much cleaner, it utilizes the fact that MATLAB (MATrix LABratory) thinks of everything as a matrix.
Since it is hard to describe MATLABdo, can you help me relate experiences of following it, or not following it?
By
Doug Hull
Doug first used MATLAB in 1994, could not figure it out until he got some help in 1995. He is now dedicated to making sure that no one else wastes a year of their life not knowing MATLAB like he did.
17:56 UTC |
Posted in Level: Advanced, Level: Basic, Topic: Off Topic, Topic: Philosophy |
Permalink |
6 Comments »
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
One thing of MATLABdo I learnt yesterday. I ashamed to say so, because I have been a frequent MATLAB user for 3 years and is considered advanced among coworkers.
The situation: You have a set of large text files on a specific format containing test data you will analyze using MATLAB. The size of the files is more than 100 MB each.
The naive solution: Write a function to read a file and extract the data you need using num2str or num2double. Use this function every time you need to access one of the files.
The MATLABdo way: Use your function once on each files. Save the variables in .mat files which is many times faster to access when using MATLAB.
This improvement shortened the execution times for a couple of scripts with about 20 minutes
This is MATLABdo
I work a lot with Matlab, and I thing that in the meanwhile I learned this MATLABdo way, but I’m sometimes surprised because this MATLABdo way is not present in the entire Matlab world…
For example, you can use 95% of the plot functions with a syntax like this:
but then you find functions like trisurf
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trisurf.html
in whoch it’s not possible to parse an axes handle.
I second J.R. – it’s really annoying when I get into the MATLABdo zone and call very similar functions that require very different syntax. If I weren’t so lazy, I guess I could write a wrapper function…
Another annoyance is when a function requires a column vector instead of a row vector. Either way it’s still a vector, can’t the function just transpose it? Probably takes less time than writing an error message…
Anyway, I was going to try to describe my favorite “MATLAB way,” (structures with dynamic field names), until I remembered that I learned it from watching Doug:
http://blogs.mathworks.com/videos/2009/02/27/dynamic-field-name-usage/
@JR!
Yes, there are some oddities like that which are hard to get rid of once they sneak in. Do we break all that code that is out there to get the consistency?
It is a tough decision that is not taken lightly.
-Thanks,
Doug
Just yesterday somebody asked on the NG the following:
“I have a matrix with the following dimensions – 13 rows x 2813 columns.
I need to multiply each column e.g (a1) with each reverse (a1′). If I do that, then a 13th x 13th matrix appears. I need to do that for all 2813 columns, so then i get 2813 matrixes.
Finally, I need to sum up these matrixes.”
Somebody came with a loop solution…but the answer was much easier A*A’!
Oleg
Forgiveness is the economy of the heart… forgiveness saves the expense of anger, the cost of hatred, the MBT Shoewaste of spirits.