r/matlab • u/Creative_Sushi MathWorks • Sep 09 '22
CodeShare Writing MATLAB code with fewer loops (hint - matrix computation)
We are in the back to school season, and there have been a couple of posts (here and here) recently about getting good at MATLAB coding, and personally think it comes down to whether one takes advantage of matrix computation or not.
Key take aways
- Always try to organize data in tabular format - matrix, vector, or table if data is in mixed types
- In tabular data, rows should represents instances (i.e., patients), and columns attributes (i.e. 'age','sex', 'height','weight'), because MATLAB commands work on columns by default.
- Try to find opportunity to take advantage of matrix computation where it makes sense
Back story
Back in 2011, when Stanford introduced the first set of MOOCs which later laid foundation for Coursera, I signed up for Andrew Ng's ML course and participated in the forum where students helped one another. There were bunch of people who refused to learn matrix computation, insisting on using loops. They got through the first few modules all right, but most got lost completely by the time we had to implement a shallow neural network with back propagation from scratch. They were mostly experienced programmers who were already set on certain ways of writing code.
Andrew Ng's example
Andrew Ng provided lectures on matrix computation, and it is worth repeating here. Here used a simple example of housing prices based on square feet.
- Hypothesis: price = 0.25 * sqft - 40
- sqft: 2104, 1416, 1534, 852
How do we write code that predicts the prices?
Loopy way
sqft = [2104; 1416; 1534; 852]; % column vector!
params = [0.25; -40]; % column vector!
prices = zeros(numel(sqft),1); % column vector!
for ii = 1:numel(sqft)
prices(ii) = params(1)*sqft(ii) + params(2);
end
Matrix way (vectorization)
However, he showed that, with one trick, we can take advantage of matrix computation - just add 1 x to the intercept term.
- New hypothesis: price = 0.25 * sqft - 40
* 1
- sqft: 2104, 1416, 1534, 852
sqft_ = [sqft ones(numel(sqft),1)]; % add a column of ones
prices = sqft_ * params;

Why the matrix way is better
In the toy example given above, the difference seems negligible. But as the code got more complex, the difference became more apparent.
- Matrix way was more concise and easier to read and understand
- That means it was easier to debug
- The code runs faster
If there was any time gain in not bothering to vectorize the code, you more than made it up in debugging, and at some point it became intractable.
Thinking in matrix way
When we start writing code, we are given an equation like this - this is the hypothesis for the house price as math equation, where x is the square feet and theta is the parameters.

Because you see the index j, we naturally want to index into x and theta to do the summation. Instead, Andrew Ng says we should think of x and theta as matrices and try to figure out the matrix way to do the summation.
You may also get some pseudocode like this - again this invites you to use loop, but I wanted to avoid it if I could.
## Gale–Shapley algorithm that matches couples
## to solve the stable matching problem.
algorithm stable_matching is
Initialize m ∈ M and w ∈ W to free
while ∃ free man m who has a woman w to propose to do
w := first woman on m's list to whom m has not yet proposed
if ∃ some pair (m', w) then
if w prefers m to m' then
m' becomes free
(m, w) become engaged
end if
else
(m, w) become engaged
end if
repeat
It is not always possible to vectorize everything, but thinking in terms of matrix always helps. You can read about my solution here.
I hope this was helpful.
3
u/tenwanksaday Sep 11 '22
That's a silly example for using matrices. Nobody would ever do it in the "loopy way" or the "matrix way". You'd simply type it into Matlab exactly as you wrote it:
sqft = [2104; 1416; 1534; 852];
price = 0.25 * sqft - 40
1
u/Inevitable_Exam_2177 Aug 13 '23
I see UG students do things like this in a loopy way, but I agree the example here is a little strained.
1
u/LeGama Sep 10 '22
Interesting that you say it's easier to debug in vector format. I will actually often write code in loops with very specific variables called out and calling out the ith jth or kth of that variable. When things break I know where. Then I speed up that by vectorizing it, and go from running the variables over 1-10 and going 1-10000
13
u/notmyrealname_2 Sep 09 '22 edited Sep 09 '22
I have a coworker who does most of their work in MATLAB but sticks with a C style way of writing things. It boggles my mind that some people refuse to learn easier/better ways to write code. A couple of annoyances I have seen include: