indiTraders - Forum for the Active Indian Trader  

Go Back   indiTraders - Forum for the Active Indian Trader > Tools > Software > Matlab

Composite Edge
Reply
 
Bookmark and Share LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 18-12-2010, 03:15 PM
Oxymoron's Avatar
Oxymoron Oxymoron is offline
Super Moderator
 
Join Date: Mar 2009
Location: Chennai
Posts: 1,275
Thanks: 4,471
Thanked 4,830 Times in 1,156 Posts
Oxymoron has a reputation beyond repute
Oxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond reputeOxymoron has a reputation beyond repute
Default Preallocating Memory in Matlab

It's probably something that most of you already know but since there are a beginners here too, I thought I'll brief on this topic.

I found this trick particularly useful when dealing with large arrays whose value is based on previous values. Saves tonnes of time in computation.

Experiment:
Start with a(1) = 1
a(2) = a(1) + 2
a(3) = a(2) + 3 and so on.

Code:
tic
a = 1;
for i = 2:100000
    a(i,1) = a(i-1,1) + i;
end
toc
Computation Time:
Elapsed time is 22.234634 seconds.

Tweak the code a bit by preallocating space to memory:
Code:
tic
a = ones(100000,1); 
% You are preallocating 100000 rows of value 1 to memory for computation in matlab 
for i = 2:100000
    a(i,1) = a(i-1,1) + i;
end
toc
Computation Time:
Elapsed time is 0.001953 seconds.

Ratio of Computation Duration:
11384.86 times
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
The Following 2 Users Say Thank You to Oxymoron For This Useful Post:
Reply

indiTraders - Forum for the Active Indian Trader > Tools > Software > Matlab


Tags
efficient coding, matlab, preallocating

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Composite Edge

All times are GMT +5.5. The time now is 09:08 PM.


vBulletin Copyright by vBulletin

Content Relevant URLs by vBSEO 3.3.2