<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-195102084416252786</id><updated>2024-09-02T07:18:25.206+05:30</updated><category term="matlab"/><category term="programming"/><category term="making matlab code faster"/><category term="file writing"/><category term="matrix"/><category term="tic-toc"/><title type='text'>Just Another Tech Blog</title><subtitle type='html'>Sharing my share of knowledge in the technical world</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://techsharingspace.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='https://techsharingspace.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>vipin</name><uri>http://www.blogger.com/profile/02146017720228354842</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-195102084416252786.post-8408374164005857884</id><published>2020-01-20T10:50:00.000+05:30</published><updated>2020-01-20T10:50:00.633+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="making matlab code faster"/><category scheme="http://www.blogger.com/atom/ns#" term="matlab"/><title type='text'>Making Matlab Code Faster - #2 Logical Indexing and Vectorizaton</title><content type='html'>Please see the second piece of code snippet from my &lt;a href=&quot;https://techsharingspace.blogspot.com/2020/01/making-matlab-code-faster-1-preallocation.html&quot; target=&quot;_blank&quot;&gt;last blog post&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
We were able to reduce the execution time of the algorithm from 66.5 msec to 36.5 msec after relying on Preallocation.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Without too much explanation let me share the code which does this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;b = a(a &amp;lt;
0.5);&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Yes, Thats it! Just one line. :)&lt;br /&gt;
&lt;br /&gt;
Let me explain the code.&lt;br /&gt;
&lt;br /&gt;
The line &lt;b&gt;(a &amp;lt; 0.5)&lt;/b&gt; creates a logical array of 1&#39;s and 0&#39;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 &#39;a&#39;.&lt;br /&gt;
&lt;br /&gt;
Suppose if a=[0.01 0.49 0.5 0.8], the logical array would be like this,&lt;br /&gt;
[1 1 0 0].&lt;br /&gt;
The 1&#39;s are equivalent to the logical value TRUE and 0&#39;s imply a logical value of FALSE.&lt;br /&gt;
&lt;br /&gt;
Now, when I write, &lt;b&gt;a(a &amp;lt; 0.5) &lt;/b&gt;it means that I am interested in only elements which have logical values of &#39;1&#39;. And these elements are assigned to array &#39;b&#39;.&lt;br /&gt;
&lt;br /&gt;
Using tic-toc timer, the average speed of this code snippet came to be around &lt;b&gt;15 msec. &lt;/b&gt;Around&amp;nbsp;60% more efficient than the non-vectorized implementation!&lt;br /&gt;
&lt;br /&gt;
I will share few lines for you to understand more about logical indexing.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;(a &amp;lt; 0.2 | a &amp;gt; 0.5)&lt;/b&gt;&amp;nbsp; will point to elements which have a value less than 0.2 OR more than 0.5.&lt;br /&gt;
The resulting logical array would look like this - [1 0 0 1].&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;(a &amp;gt; 0.2 &amp;amp; a &amp;lt; 0.5)&lt;/b&gt;&amp;nbsp; will point to elements which have a value more than 0.2 AND less than 0.5.&lt;br /&gt;
The resulting logical array would look like this - [0 1 0 0].&lt;br /&gt;
&lt;br /&gt;
&amp;amp; and | are logical array operators.</content><link rel='replies' type='application/atom+xml' href='https://techsharingspace.blogspot.com/feeds/8408374164005857884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/making-matlab-code-faster-2-logical.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/8408374164005857884'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/8408374164005857884'/><link rel='alternate' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/making-matlab-code-faster-2-logical.html' title='Making Matlab Code Faster - #2 Logical Indexing and Vectorizaton'/><author><name>vipin</name><uri>http://www.blogger.com/profile/02146017720228354842</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-195102084416252786.post-5029950652615001289</id><published>2020-01-20T09:44:00.000+05:30</published><updated>2020-01-20T09:44:05.501+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="making matlab code faster"/><category scheme="http://www.blogger.com/atom/ns#" term="matlab"/><title type='text'>Making Matlab Code Faster - #1 Preallocation </title><content type='html'>This is the first post in the series of Making Matlab Code Faster series.&lt;br /&gt;
&lt;br /&gt;
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?&lt;br /&gt;
&lt;br /&gt;
Why not make the necessary changes to see if your code can perform better. There are few things you can do towards this goal. &lt;b&gt;Preallocation &lt;/b&gt;is just one of them.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s look at an example:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt; line-height: 115%;&quot;&gt;a=rand(1000000,1);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;%Declare a random matrix with 1 million elements&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;b=0;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;j=1;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt; i=1:size(a,1)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;%run the loop for the all the elements in &#39;a&#39;.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;(a(i) &amp;lt; 0.5)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;%if the current element is less than 0.5 save it in an array &#39;b&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;b(j)=a(i);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;j=j+1;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 13.3333px;&quot;&gt;%increment j to save next element in the next location in &#39;b&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
We can see that initially array &#39;b&#39; is single element matrix. But as time goes on, we keep incrementing the size of the array.&lt;br /&gt;
&lt;br /&gt;
This is not an efficient approach. Repeatedly resizing arrays often requires MATLAB to spend extra time looking for larger contiguous blocks of memory, and then moving the array into those blocks.&lt;br /&gt;
&lt;br /&gt;
So how can we solve this issue? We can Preallocate the space for &#39;b&#39;.&lt;br /&gt;
&lt;br /&gt;
You would say that, you don&#39;t know what the end size of &#39;b&#39; would be. In such a case, just preallocate anyway with the maximum size possible and then at the end of processing just remove the unused space.&lt;br /&gt;
&lt;br /&gt;
See the solution here:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;b=zeros(size(a));&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 13.3333px;&quot;&gt;%Preallocate with the maximum size possible for &#39;b&#39;.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;j=1;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt; i=1:size(a,1)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;(a(i) &amp;lt; 0.5)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b(j)=a(i);&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;j=j+1;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: black; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;b(j:end)=[];&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 13.3333px;&quot;&gt;%remove the unused elements at the end.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 13.3333px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
Using the&amp;nbsp;&lt;a href=&quot;https://techsharingspace.blogspot.com/2020/01/find-execution-time-in-matlab.html&quot; target=&quot;_blank&quot;&gt;tic-toc timers&lt;/a&gt;,&amp;nbsp;I measured the average time for these two code snippets. The result is:&lt;br /&gt;
&lt;br /&gt;
For the first approach without Preallocation , it takes &lt;b&gt;66.5 msec&lt;/b&gt;. while,&lt;br /&gt;
for the second apprach with Preallocation it only took &lt;b&gt;36.5 msec.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Almost 50% efficiency with just a single step!&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='https://techsharingspace.blogspot.com/feeds/5029950652615001289/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/making-matlab-code-faster-1-preallocation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/5029950652615001289'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/5029950652615001289'/><link rel='alternate' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/making-matlab-code-faster-1-preallocation.html' title='Making Matlab Code Faster - #1 Preallocation '/><author><name>vipin</name><uri>http://www.blogger.com/profile/02146017720228354842</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-195102084416252786.post-8614332041495762167</id><published>2020-01-12T20:03:00.000+05:30</published><updated>2020-01-12T20:03:31.825+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="file writing"/><category scheme="http://www.blogger.com/atom/ns#" term="matlab"/><category scheme="http://www.blogger.com/atom/ns#" term="programming"/><title type='text'>Creating a Logfile for your project in Matlab</title><content type='html'>If you have a complicated project with many functions interconnected with each other, debugging could be a pain in the neck. To make it a bit more easier, I personally write &quot;current status&quot; of the code to a text file.&lt;br /&gt;
&lt;br /&gt;
Every time a specific event is caused, I write to the text file, along with the timestamp of the message. This helps me often, to pin point the mistake in my code.&lt;br /&gt;
&lt;br /&gt;
This is fairly simple thing to do. See the below function, write_log.m&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt; write_log(msg)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;fid =
fopen(fullfile(pwd, &lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&#39;LogFile.txt&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt; fid == -1&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp; error(&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&#39;Cannot open log
file.&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;fprintf(fid,
&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&#39;%s: %s\r\n&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;, datestr(now, 0),
msg);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;Courier New&amp;quot;; font-size: 13.3333px;&quot;&gt;%this line could be changed to add more details to the message&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;fclose(fid);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;a href=&quot;https://in.mathworks.com/help/matlab/ref/pwd.html&quot; target=&quot;_blank&quot;&gt;pwd &lt;/a&gt;stands for present working directory. I want to save the LogFile in the same directory as the current project.&lt;br /&gt;
Every time the message, indicated by the variable &lt;b&gt;msg &lt;/b&gt;here, is written to the text file, I precede it with the current date and time.&lt;br /&gt;
&lt;br /&gt;
Lets make a few example calls to the function and see what the text file contains.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;write_log(&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&#39;starting the project&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;pause(2);&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;%wait 2
seconds.&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;write_log(&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-language: ML;&quot;&gt;&#39;No error yet&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 12.0pt; mso-bidi-font-family: Kartika; mso-bidi-language: ML;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;pause(1);&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;%wait 1 second.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;write_log(&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;&#39;Alright, time to see the results!&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
Taking a look at the LogFile, we see the following three lines:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
12-Jan-2020 19:51:12: starting the project&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
12-Jan-2020 19:51:14: No error yet&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
12-Jan-2020 19:51:15: Alright, time to see the results!&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Notice the timestamps and their delays from each other.&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='https://techsharingspace.blogspot.com/feeds/8614332041495762167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/creating-logfile-for-your-project-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/8614332041495762167'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/8614332041495762167'/><link rel='alternate' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/creating-logfile-for-your-project-in.html' title='Creating a Logfile for your project in Matlab'/><author><name>vipin</name><uri>http://www.blogger.com/profile/02146017720228354842</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-195102084416252786.post-715918942231102986</id><published>2020-01-12T12:58:00.000+05:30</published><updated>2020-01-12T13:00:19.115+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="matlab"/><category scheme="http://www.blogger.com/atom/ns#" term="programming"/><title type='text'>Checking if two arrays are equal or not in Matlab</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
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?&lt;br /&gt;
&lt;br /&gt;
In such a case a simple == doesn&#39;t work. In such a case, the function &lt;b&gt;isequal &lt;/b&gt;helps us to achieve this.&lt;br /&gt;
&lt;br /&gt;
The usage is pretty simple, as you can see in the below examples...&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 1:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
A = zeros(2,2)+1e-10;&lt;br /&gt;
B = zeros(2,2);&lt;br /&gt;
check = isequal(A,B)&lt;br /&gt;
&lt;br /&gt;
A and B differ by a small fraction, but still the variable check shows the value &quot;0&quot; after running the snippet.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 2:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
A = zeros(2,2);&lt;br /&gt;
B = zeros(3,2);&lt;br /&gt;
check = isequal(A,B)&lt;br /&gt;
&lt;br /&gt;
In the above case, both the arrays A and B have zero, but their size is different. Hence the answer, check shows &quot;0&quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 3:&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;A = struct(&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&#39;field1&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;,0.05,&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&#39;field2&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;,200);&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;B = struct(&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&#39;field2&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;,200,&lt;/span&gt;&lt;span style=&quot;color: #a020f0; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;&#39;field1&#39;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;,0.05);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;check =
isequal(A,B)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
The structures are declared in the first two lines. You can see that, fundamentally both A and B are the same, even though the way they declared are a bit different. Guess what the value of &quot;check&quot; is after running the snippet?&amp;nbsp; &quot;1&quot; !&lt;br /&gt;
&lt;br /&gt;
Note that,&amp;nbsp;isequal function does &lt;b&gt;NOT&lt;/b&gt; treat NaN values as equal to each other. So make sure you take this fact into account when using isequal.&lt;br /&gt;
&lt;br /&gt;
More information on this function &lt;a href=&quot;https://in.mathworks.com/help/matlab/ref/isequal.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://techsharingspace.blogspot.com/feeds/715918942231102986/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/checking-if-two-arrays-are-equal-or-not-in-matlab.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/715918942231102986'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/715918942231102986'/><link rel='alternate' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/checking-if-two-arrays-are-equal-or-not-in-matlab.html' title='Checking if two arrays are equal or not in Matlab'/><author><name>vipin</name><uri>http://www.blogger.com/profile/02146017720228354842</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-195102084416252786.post-6835112031517576077</id><published>2020-01-12T07:50:00.000+05:30</published><updated>2020-01-12T07:51:37.058+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="matlab"/><category scheme="http://www.blogger.com/atom/ns#" term="matrix"/><category scheme="http://www.blogger.com/atom/ns#" term="programming"/><category scheme="http://www.blogger.com/atom/ns#" term="tic-toc"/><title type='text'>Find Execution Time In Matlab</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
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.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
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 &lt;b&gt;tic &lt;/b&gt;and &lt;b&gt;toc &lt;/b&gt;can be used for this.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
See the example below.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;tic&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;%add this
statement just before the start of the code you want to check&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;a=rand(100);&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;%create 100
by 100 random matrix.&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt; i=1:10&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;%repeat 10 times&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a=a*a;&amp;nbsp;
&lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;%matrix multiplication&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot;; font-size: 10.0pt;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;toc&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;%add this
statement just after the end of the code you want to check&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;co1&quot; style=&quot;color: forestgreen; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: 13.464px; white-space: nowrap;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
The code is self explanatory. Running the code in Matlab 2016b, a message similar to the following will be shown on Command Window.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Elapsed time is 0.045149 seconds.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Pretty simple isn&#39;t it.&amp;nbsp; :)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://techsharingspace.blogspot.com/feeds/6835112031517576077/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/find-execution-time-in-matlab.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/6835112031517576077'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/195102084416252786/posts/default/6835112031517576077'/><link rel='alternate' type='text/html' href='https://techsharingspace.blogspot.com/2020/01/find-execution-time-in-matlab.html' title='Find Execution Time In Matlab'/><author><name>vipin</name><uri>http://www.blogger.com/profile/02146017720228354842</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>