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:
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.
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
Post a Comment