How can I improve the speed of the following code

4 visualizaciones (últimos 30 días)
Hassan
Hassan el 17 de Ag. de 2018
Respondida: OCDER el 20 de Ag. de 2018
This is the .m file here.
  4 comentarios
Hassan
Hassan el 18 de Ag. de 2018
This improves the code but not so much. I have uploaded the .m file and you will see in this file that I am trying to solve a problem for 100 cells and in every cell, I have either a 3 by 3 matrix of a 3 by 1 vector. In my final result for D, I obtain a 100 cells with 2 by 2 matrix which can vary in every cell. I then convert this to a vector of 200 by 2 that I use in a bigger code. Ia m just wondering if anyone has suggestion of converting the cells and doing ordinary array operations rather than cell operations.
Thanks
Jan
Jan el 20 de Ag. de 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.

Iniciar sesión para comentar.

Respuestas (1)

OCDER
OCDER el 20 de Ag. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by