- upload all required data in one .mat file by clicking the paperclip button.
- paste the code as text. We cannot run a screenshot. We cannot edit a screenshot. We cannot search a screenshot. A screenshot is very pretty, but basically useless to us.
How can I improve the speed of the following code
2 次查看(过去 30 天)
显示 更早的评论
This is the .m file here.
4 个评论
Jan
2018-8-20
@Hassan: Your idea sounds perfect: Omit the cells, if you do not need them. Cells are required, if the stores arrays have different types are sized. Is this true in your case? "in every cell, I have either a 3 by 3 matrix of a 3 by 1 vector" is not clear.
回答(1 个)
OCDER
2018-8-20
I see you're using a lot of cellfun and nested for loops. By NOT using cell arrays, you could just vectorized math it seems. cellfun could be slower than normal for loops. Also, with normal for loop on cell arrays, you could convert to parfor loop. But try parfor last.
EXAMPLE:
a = rand(200);
b = rand(200);
A = num2cell(a);
B = num2cell(b);
tic
c = a.*b;
toc %0.0102 s
tic
C = cellfun(@times, A, B, 'un', 0);
toc %0.0670 s
tic
D = cell(size(A));
for j = 1:numel(A) %could be parfor j = 1:numel(A)
D{j} = A{j}*B{j};
end
toc %0.0459 s
Use cellfun for doing simple stuff like cellfun('isempty', X). Note that using the function handle "@" in cellfun is much slower.
X = num2cell(rand(200));
tic; cellfun(@isempty, X) ; toc; %0.0340 s
tic; cellfun('isempty', X); toc; %0.0005 s
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!