Effacer les filtres
Effacer les filtres

Vectorize 3 for loops

2 vues (au cours des 30 derniers jours)
Tim
Tim le 24 Août 2012
Hello,
I have to do a large calculation and right now I use 3 for loops to do it....but I wonder if I could use vectorization to make it faster.
my code:
PermPolynom = allVL1(dim,2,'==',inf);
lenghtPermPolynom = size(PermPolDiag,1);
for n = 1:lenght_X2
for k = 1:lenght_X1
for l = 1:lengthPermPolynom
DiagVek(k,n) = DiagVek(k,n)-(prod((X2(n,:) X1(k,:)).^ PermPolynom(l,:)));
end
end
end
X1 and X2 are matrices with different number of rows but same number of columns (every row stands for a multidimensional vector of length dim).
For example:
X1 = [0 2 3 2 3; 2 3 4 1 3; 3 4 5 4 3;.....;4 5 2 1 2] -> dim = 5
PermPolynom gives me all possible Permutations for a vector of length dim so that the sum of all entries in the vector is 2.
example for dim 3 and sum = 1:
[0 0 1; 0 1 0; 1 0 0]
The goal is to calculate for every entry in X1 and X2 the multidimensional polynomial.
I tried to vectorize it like it is said in a help of matlab:
i = 0;
for t = 0:.01:10
i = i + 1;
y(i) = sin(t);
end
A vectorized version of the same code is
t = 0:.01:10;
y = sin(t);
but that doesn't work...I think because of the different length of X1 and X2.
Is there a way to get rid of the loops?
Thanks Tim
  2 commentaires
Jan
Jan le 24 Août 2012
The expression is not complete:
DiagVek(k,n)-(prod((X2(n,:) ??? X1(k,:)).^ PermPolynom(l,:)));
What does appear instead of the ???
Tim
Tim le 24 Août 2012
a minus...strange that it doesn't appear...

Connectez-vous pour commenter.

Réponse acceptée

Daniel Shub
Daniel Shub le 24 Août 2012
Loops are no longer painfully slow in MATLAB and haven't been for quite some time. A better question would be how can I speed up my code. For this we need more than just the code, we also need details about the dataset (how big is it). Have you run your code through mlint and the profiler?
It doesn't look like you have preallocated DiagVek. While there have been improvements in MATLAB's dynamic memory allocation, preallocation still tends to be very important.
  2 commentaires
Tim
Tim le 24 Août 2012
Good to know that loops aren't so slow. :)
The vecors X1 and X2 are 5000x(three to fifteen) matrices and preallocated every vector or matric in my code. I already used mlint. It told my that there are 3 matrices that change size in every loop and that I should preallocate them but in these cases this is not possible because I need them in different sizes every iteration. There is nothing more that mlint tells me.
I didn't use profiler yet.
The thing is that the code above is only the first matrice I have to calculate and the whole code is much longer. It is probably to much to ask to look through all the 90 lines of code I wrote ^^
I just thought that replacing the loops could help a bit because I have several calculations which look the same as above :)
Thank you for your quick answer and your help.
Tim
Daniel Shub
Daniel Shub le 24 Août 2012
That mlint warning is key and despite needing the matrix to be a different size every iteration, suggests you are doing something wrong. You should take a look at cell arrays and/or consider simply overwriting the matrix on every iteration instead of saving all the different size matrices.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by