If you have ever used Matlab for your university projects or research simulations, you know that it has unlimited potential and flexibility in simulating almost any problem you want. More importantly, the user interface is easy to learn and the code is easy to grasp by beginners.

I started coding in Matlab about 4 years ago, and the journey has been an exciting and one with full of learning. I started coding on Matlab for my course projects during my bachelors, and now I use it full time for my PhD thesis research. There were several times during my journey, when I had to wait several hours to days for my simulations to converge. I always wondered — how could I make this fast, how can I make the for loops to iterate faster? Moreover, some of my work required a distributed setup, that is different versions of Matlab talking to each other, exchanging information and running algorithms to perform certain task. I always wondered — how could I do this? How do I make my code cool enough to be realistic?

In this article, I will share some useful programming tips which I learnt over my 4 years of coding and playing around with Matlab. Let’s get started !

1. Faster for-loops

For loops are probably one of the most widely used blocks of code in Matlab. For running iterative algorithms, calculating matrix products over a set of data or simulating a system over time, for loops are used everywhere. For loops compute each iteration sequentially, making it slow over large number of iterations and massive computations. Here comes Matlab’s Parallel Computing Toolbox to the rescue.

For example, consider this very simple example where I need to compute the element-wise sum of the entries in matrix a and b**, **and store the result in c.

a = randn(1e7,1e7);
b = randn(1e7,1e7);
c = zeros(1e7,1e7);

for i=1:1e7
    for j=1:1e7
      c(i,j) = a(i,j) + b(i,j);
    end
end

This for loop computation takes about 7.72 seconds to complete in my testing. Let’s see how we can use the parallel for loop here.

First, declare the number of workers, which can maximum be the number of hyper-threaded cores your processor has.

parpool(4); %declare number of workers (max=cores) 

parfor i=1:1e7 %use parfor instead of for
    for j=1:1e7
      c(i,j) = a(i,j) + b(i,j);
    end
end
delete(gcp('nocreate')); %close parallel computing

Note that nested for loops are not allowed, so we change one of the for loops to parallel loop, and keep the inner loop as same. The same task now takes just 1.36 seconds. Parallel computation toolbox is not only restricted to for loops, but has a variety of helpful features to enable your simulations to complete faster, so that you can get a good night sleep.

2. Progress bar (with ETA)

Have you ever waited wondering how much longer it will take for your code to finish execution ? I am sure you have tried various options, one of which is printing out the iteration number of a loop, and then make a guesstimate as to how much longer it will take to finish. Well, do that no more !

You can use this add-on to all your while and for-loops, to not only see the percentage of progress made, but also get an estimated time of completion. It is really great and I have been using it for all of my Matlab projects. The usage is as simple as shown below.

h = waitbar(0,'Please wait...'); %handle for the waitbar

for i=1:1e7
c(i) = a(i) + b(i);
waitbar(i/1e7,h); %update waitbar status
end

Image for post

Credits : Mathworks

You will get a progress bar as shown in the image with the “progress percentage” and “estimated time remaining”.


3. Distributed algorithms or shared data

Distributed algorithms are algorithms which work on different computers to perform a specific task together. Especially large systems, use distributed algorithms to make such algorithms faster and less intensive on a single processor.** Such setup also requires data to be shared among different computers.**

However, most of the time while simulating such algorithms, we just write a single piece of code which sequentially runs on one Matlab environment. Though this may produce correct final result, but it becomes difficult to visualize how it would converge when implemented in a real distributed setup. I will show you how I discovered to make this cool and realistic using simple pieces of code.

#matlab #research #coding #programming #experience #visual studio code

Make Your Matlab Codes Faster and More Realistic with These Tips
1.25 GEEK