improve efficiency of a matlab code that includes for loop and if statement

1 回表示 (過去 30 日間)
Locks
Locks 2013 年 4 月 22 日
I have the followig script which I am running for a matrix that consists of several thousand rows and which makes the full ccode very slow. Is there any way to improve the speed of the code?
Here you find the code which is saved in a seperate script and which is called by the function run the speciific script:
dataset=zeros(size(data9,1),9);
dataset(:,1:9)=data9;for i=1:length(dataset(:,1))
%h=msgbox(num2str(i))
if dataset(i,3)==1
vola(i)=blkimpv(dataset(i,1),dataset(i,2),dataset(i,7),dataset(i,5),dataset(i,4)); elseif dataset(i,3)==2
vola(i)=blkimpv(dataset(i,1),dataset(i,2),dataset(i,7),dataset(i,5),dataset(i,4),10,[],{'Put'});
end
end
Which improvements are there to get the code execution the desired functions much faster?

採用された回答

Cedric
Cedric 2013 年 4 月 22 日
編集済み: Cedric 2013 年 4 月 22 日
It really depends whether BLKIMPV can work on vectors. If so, you can go for something like:
dataset = zeros(size(data9,1), 9) ;
dataset(:,1:9) = data9;
vola = zeros(size(dataset,1), 1) ;
id = dataset(i,3) == 1 ;
vola(id) = blkimpv(dataset(id,1), dataset(id,2), dataset(id,7), ...
dataset(id,5), dataset(id,4)) ;
id = dataset(i,3) == 2 ;
vola(id) = blkimpv(dataset(id,1), dataset(id,2), dataset(id,7), ...
dataset(id,5), dataset(id,4), 10, [], {'Put'}) ;
where you might have to replace 10 and {'Put'} with arrays of these values.
I don't have the Financial Toolbox though, so I can't test.
  6 件のコメント
Locks
Locks 2013 年 4 月 23 日
ok, I will try that. thanks for your help!
Cedric
Cedric 2013 年 4 月 23 日
You're welcome! Try to use the profiler on a regular basis in the beginning (even when you don't really need to); it will give you a lot of insights into your code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by