- 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
7 views (last 30 days)
Show older comments
This is the .m file here.
4 Comments
Jan
on 20 Aug 2018
@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.
Answers (1)
OCDER
on 20 Aug 2018
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!