Skip to main content

Making Matlab Code Faster - #2 Logical Indexing and Vectorizaton

Please see the second piece of code snippet from my last blog post.

We were able to reduce the execution time of the algorithm from 66.5 msec to 36.5 msec after relying on Preallocation.

In this post I want to work on the same algorithm, and see if I can make it more efficient. For this I want to rely on the power of logical expressions and indexing in Matlab.

Without too much explanation let me share the code which does this:

b = a(a < 0.5);

Yes, Thats it! Just one line. :)

Let me explain the code.

The line (a < 0.5) creates a logical array of 1's and 0's depending on whether the element is less than 0.5 or not. The size of this logical array is same as the size of array 'a'.

Suppose if a=[0.01 0.49 0.5 0.8], the logical array would be like this,
[1 1 0 0].
The 1's are equivalent to the logical value TRUE and 0's imply a logical value of FALSE.

Now, when I write, a(a < 0.5) it means that I am interested in only elements which have logical values of '1'. And these elements are assigned to array 'b'.

Using tic-toc timer, the average speed of this code snippet came to be around 15 msec. Around 60% more efficient than the non-vectorized implementation!

I will share few lines for you to understand more about logical indexing.

(a < 0.2 | a > 0.5)  will point to elements which have a value less than 0.2 OR more than 0.5.
The resulting logical array would look like this - [1 0 0 1].

(a > 0.2 & a < 0.5)  will point to elements which have a value more than 0.2 AND less than 0.5.
The resulting logical array would look like this - [0 1 0 0].

& and | are logical array operators.

Comments

Popular posts from this blog

Checking if two arrays are equal or not in Matlab

You might already know that to see if two variables are equal or not, we could use the == operator. But what about if you want to check two arrays or two structures are similar or not? In such a case a simple == doesn't work. In such a case, the function isequal helps us to achieve this. The usage is pretty simple, as you can see in the below examples... Example 1: A = zeros(2,2)+1e-10; B = zeros(2,2); check = isequal(A,B) A and B differ by a small fraction, but still the variable check shows the value "0" after running the snippet. Example 2: A = zeros(2,2); B = zeros(3,2); check = isequal(A,B) In the above case, both the arrays A and B have zero, but their size is different. Hence the answer, check shows "0". Example 3: A = struct( 'field1' ,0.05, 'field2' ,200); B = struct( 'field2' ,200, 'field1' ,0.05); check = isequal(A,B) The structures are declared in the first two lines. You can see that, f...

Making Matlab Code Faster - #1 Preallocation

This is the first post in the series of Making Matlab Code Faster series. As you might have realized already, getting stuff somehow to get to work, might be a fairly simple task with Matlab. But why stop there? Why not make the necessary changes to see if your code can perform better. There are few things you can do towards this goal. Preallocation is just one of them. Let's look at an example: a=rand(1000000,1);   %Declare a random matrix with 1 million elements b=0;  j=1; for i=1:size(a,1)   %run the loop for the all the elements in 'a'.      if (a(i) < 0.5)   %if the current element is less than 0.5 save it in an array 'b'          b(j)=a(i);         j=j+1;   %increment j to save next element in the next location in 'b'     end end We can see that initially array 'b' is single element matrix. But as time goes on, we keep ...

Find Execution Time In Matlab

MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and proprietary programming language developed by MathWorks. It still amazes me how easily scientific calculations can be done with the help of this tool. Sometimes you might want to know, how fast or slow is the piece of code you have written. This is very easy with matlab. The built-in function tic and toc can be used for this.  See the example below. tic  %add this statement just before the start of the code you want to check a=rand(100);  %create 100 by 100 random matrix. for i=1:10  %repeat 10 times     a=a*a;  %matrix multiplication end toc  %add this statement just after the end of the code you want to check The code is self explanatory. Running the code in Matlab 2016b, a message similar to the following will be shown on Command Window. Elapsed time is 0.045149 seconds. Pretty simple isn't it.  :)