If function with cellfun (i.e. vectorized code instead of ridiculously slow loop).

Hello everyone. I am new to matlab and really try to optimize my code since it takes to much time to run. I don't really understand the concept behind cellfun yet. If we take the following example:
I would have no idea how to write it in vectorized code since I have to go through every cells and then through every lines of each cell.
So is it possible to write it in vectorized code and if yes can anyone help me with that ?

6 件のコメント

Matt J
Matt J 2018 年 3 月 14 日
Using cellfun isn't the same as vectorizing and won't make things faster. It just hides the for-loop. Your loops over k can be vectorized, but to help us show you how, you should replace the image of your code with actual text that we can copy/paste.
Stephen23
Stephen23 2018 年 3 月 14 日
編集済み: Stephen23 2018 年 3 月 14 日
@Pierre Lonfat: there are many steps and tools to speeding up code, vectorization is just one of them, for example it might be required to preallocate the arrays that you are accessing using indexing. Without seeing your actual code it is hard to give any more advice than this. The use of clear is also likely not required, and might slow your code down. Vectorization is unrelated to cellfun.
Pierre Lonfat
Pierre Lonfat 2018 年 3 月 14 日
編集済み: Pierre Lonfat 2018 年 3 月 14 日
Yes sure ! Sorry for the stupid screen shot and waste of time ! I will copy/past my codes next time ;)
for j=1:rolf.initial.indices %j=40 in this case
for k=1:length(rolf.daily.data{1,j})
if rolf.daily.short_term{1,j}(k,1)<=1
rolf.daily.forecast{1,j}(k,1)=-1;
else rolf.daily.forecast{1,j}(k,1)=1;
end
if rolf.daily.performance{1,j}(k,1)<=0;
rolf.daily.control{1,j}(k,1)=-1;
else rolf.daily.control{1,j}(k,1)=1;
end
end
end
Thank you very much in advance!
Stephen23
Stephen23 2018 年 3 月 14 日
@Pierre Lonfat: thank you for posting the code as text. However it is still not enough for us to give advice on how to speed it up: it is also important what happens before this code, and/or how you are calling it.
Adam
Adam 2018 年 3 月 14 日
cellfun is rarely, if ever, faster than a loop, it is just a different way of doing it which may be considered neater or may not. Moving away from using cells in the first place is usually a big step towards speeding up code if you can possibly use numeric arrays instead.
Guillaume
Guillaume 2018 年 3 月 14 日
You're using 2D indexing {row, col} with one of the dimension looking like it's always 1 as opposed to linear indexing {idx}. Is your data actually 2D or 1D?
The difference in speed between the two is negligible but I personally find it confusing when people use 2D indexing with 1D data. Plus, this add a hidden assumption that the cell array is a row vector and the content of each cell is a column vector, whereas with linear indexing the code works regardless of the direction of the vector.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2018 年 3 月 14 日

コメント済み:

2018 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by