Hi, I have the following code that I need to vectorize to compute faster:
Here,
neq = 3197;
length(K1) = 152;
d1 is such d1{1,i} = 3197 by 1;
delNonZero = 152 by 1;
cd = zeros(neq,neq);
for i = 1:length(K1)
for j = 1:length(K1)
cd =cd+ d1{1,i}*d1{1,j}'*mean([delNonZero(i) delNonZero(j)]);
end
end

4 comentarios

Bob Thompson
Bob Thompson el 28 de Feb. de 2019
By 'vectorize' are you asking for us to help you remove one or more of the loops, or are you asking to help you multiply your large arrays together within the loops?
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman el 28 de Feb. de 2019
Hi Bob, I just want it to compute faster. Possibly by eliminating for loops. Sorry for the confusion.
Bob Thompson
Bob Thompson el 28 de Feb. de 2019
Got it. I am not an expert in those things, so hopefully somebody else can offer you a better solution. I will say that I don't know if you can eliminate the loops because of your desire to look at just a few elements at a time, rather than just looking at entire rows or the like.
Mohammod Minhajur Rahman
Mohammod Minhajur Rahman el 28 de Feb. de 2019
Hi Bob, thank you for your time. I would wait for a solution while trying by myself.

Iniciar sesión para comentar.

 Respuesta aceptada

Jos (10584)
Jos (10584) el 28 de Feb. de 2019
Editada: Jos (10584) el 28 de Feb. de 2019

0 votos

Some suggestions:
  • replace mean(A,B) by (A+B)/2
  • you can have j run from i to length(K1), since everything seems symmetric (unless there are complex numbers involved):
for i = 1:..
cd = cd + d1{i,1}*d1{i,1}'*delNonZero(i) ;
for j = i+1: ..
cd = cd + 2*d1{i,1}*d1{j,1}'*(delNonZero(i)+delNonZero(j))/2; % you can remove the 2's !!
end
end
  • do not use the function CD as avariable name ...

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by