how can I increase the performance of this loop?

1 visualización (últimos 30 días)
SA-W
SA-W el 11 de Jul. de 2023
Comentada: SA-W el 11 de Jul. de 2023
nPts = 15;
a = linspace(0.1, 2.0, nPts);
b = linspace(0.1, 2.0, nPts);
c = linspace(0.1, 2.0, nPts);
d = linspace(-0.3, 2.0, nPts);
e = linspace(-0.3, 2.0, nPts);
f = linspace(-0.3, 2.0, nPts);
res = zeros(length(a)*length(b)*length(c)*length(d)*length(e)*length(f), 2);
idx = 1;
for i=a
for j=b
for k=c
for l=d
for m=e
for n=f
C = [i l m; l j n; m n k];
% do somthing with C and store a row in "res"
detF = sqrt(det(C));
if detF<0.001
res(idx,:)=NaN;
else
Cbb = detF^(-2/3).*C;
res(idx, 1) = trace(Cbb);
res(idx, 2) = trace(inv(Cbb));
end
% increment counter
idx = idx +1 ;
end
end
end
end
end
end
%remove the rows with NaN's
The above code works, but is probably not a good implementation.
How can I increase the performance of this code? My objective is to build all combinations of the elements in the vectors a,b,c,d,e,f , then built a symmetric matrix for each combination as shown, compute the first two principal invariants of the (scaled matrix), and store it in a result variable and visualize it finally in a scatter plot.
  2 comentarios
Stephen23
Stephen23 el 11 de Jul. de 2023
"How can I increase the performance of this code?"
Perhaps by changing the part labeled 'do somthing with C and store a row in "res" so that it can process arrays:
or rewriting it using more efficient code practices:
Or perhaps by using parallel processing, or lots of other ways... without knowing the details of what that "somthing" is, we can't do much more for you.
SA-W
SA-W el 11 de Jul. de 2023
Sorry for not being more specific. I edited my question to make the intention more clear.
The matrix is C symmetric, and I want to figure out what values the first two principal invariants can take be restricting the elements of the matrix somehow.
Does this help to provide more assistance?

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 11 de Jul. de 2023
nPts = 15;
a = linspace(0.1, 2.0, nPts);
b = linspace(0.1, 2.0, nPts);
c = linspace(0.1, 2.0, nPts);
d = linspace(-0.3, 2.0, nPts);
e = linspace(-0.3, 2.0, nPts);
f = linspace(-0.3, 2.0, nPts);
sizeOfResult = [nPts^6, 1]
sizeOfResult = 1×2
11390625 1
So you want all 11.4 million results? If you're using release R2023a or later, take a look at the combinations function.
results = combinations(a, b, c, d, e, f);
size(results)
ans = 1×2
11390625 6
Let's look at the first couple rows of results
head(results)
a b c d e f ___ ___ ___ ____ ____ ________ 0.1 0.1 0.1 -0.3 -0.3 -0.3 0.1 0.1 0.1 -0.3 -0.3 -0.13571 0.1 0.1 0.1 -0.3 -0.3 0.028571 0.1 0.1 0.1 -0.3 -0.3 0.19286 0.1 0.1 0.1 -0.3 -0.3 0.35714 0.1 0.1 0.1 -0.3 -0.3 0.52143 0.1 0.1 0.1 -0.3 -0.3 0.68571 0.1 0.1 0.1 -0.3 -0.3 0.85
If you need them as a numeric array rather than as a table:
data = results.Variables;
head(data)
0.1000 0.1000 0.1000 -0.3000 -0.3000 -0.3000 0.1000 0.1000 0.1000 -0.3000 -0.3000 -0.1357 0.1000 0.1000 0.1000 -0.3000 -0.3000 0.0286 0.1000 0.1000 0.1000 -0.3000 -0.3000 0.1929 0.1000 0.1000 0.1000 -0.3000 -0.3000 0.3571 0.1000 0.1000 0.1000 -0.3000 -0.3000 0.5214 0.1000 0.1000 0.1000 -0.3000 -0.3000 0.6857 0.1000 0.1000 0.1000 -0.3000 -0.3000 0.8500
Without knowing more about what the "somthing" you want to do is I'm not sure how much more we'll be able to suggest. With this approach you may be able to use parfor to parallelize operating on each row of this data.
Alternately you could reshape the columns of data then use those columns to create a 3-by-3-by-11390625 array and finally use some of the page-based functions (like pageeig) to operate on each of the pages of that array.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by