How can I improve the speed of the following code
Ältere Kommentare anzeigen
This is the .m file here.
4 Kommentare
@Hassen: are all those cell arrays really required for storing numeric data? Lots of simple numeric operations, these are just calling out to use numeric arrays and some simple vectorized code! However, without knowing anything about your data we are very limited in how to help you. If you want help then please do both of these:
- 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"
By following the advice in the MATLAB documentation:
At the very least you should move every calculation to the lowest level you can. So those two lines in your else block belong only in the m for-loop, as they are only dependent on m.
The same goes for the -1/Ds{m,1}(i,sizeVx) term. You can move that to the i-loop and save it in a temporary variable.
As another note: you should consider replacing i and j as loop indexes, as they cause bug where you are not careful enough, because they also work as they imaginary unit.
And put your code here as code instead of an image. Select the code in this editor and push the {}Code button for correct formatting.
Hassan
am 18 Aug. 2018
Jan
am 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.
Antworten (1)
OCDER
am 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
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!