How can I avoid loop for code optimization?

y = linspace(1,10,1000);
for m = 1:length(Y)
Z = [1 sin(Y(m)); 2 cos(Y(m))];
X(m) = det(Z);
end

4 commentaires

Walter Roberson
Walter Roberson le 28 Avr 2021
Modifié(e) : Walter Roberson le 28 Avr 2021
What overall size of output are you looking for over the double loop?
What size of output are you looking for to be generated within the outer loop?
... The way you are coding at the moment, you could replace both loops with the simple
A = [11 10000000; 25 10000];
unless you were deliberately wasting time by doing the double loop.
Bhromzara
Bhromzara le 29 Avr 2021
@ Walter Roberson
Please check my question again. I put some changes there for more clarity of the question.
X = -Y; % det of Z
Bhromzara
Bhromzara le 29 Avr 2021
@Bruno Luong assume : Z = [1 sin(Y(m)); 2 cos(Y(m))];

Connectez-vous pour commenter.

Réponses (2)

Bruno Luong
Bruno Luong le 29 Avr 2021
Modifié(e) : Bruno Luong le 29 Avr 2021
Use MultipleQR FEX (C compiler for MEX build is required, unless for Windows platform)
y = linspace(1,10,1000);
Y = reshape(y,1,1,[]);
z = zeros(size(Y));
Z = [1+z sin(Y);
2+z cos(Y)];
n = size(Z,1);
[~,R]=MultipleQR(Z);
R = reshape(R,n*n,[]);
X = prod(R(1:n+1:end,:),1)*(-1)^n;
plot(y,X)
Jan
Jan le 29 Avr 2021
Modifié(e) : Jan le 29 Avr 2021
Start with pre-allocating the output. Letting an array grow iteratively consumes a lot of resources, because Matlab has to create a new vector in each iteration.
Y = linspace(1,10,1000);
X = zeros(1, length(Y)); % Pre-allocation!!!
for m = 1:length(Y)
Z = [1, sin(Y(m)); 2, cos(Y(m))];
X(m) = det(Z);
end
The code can be simplified and no loop is required:
Y = linspace(1, 10, 1000);
X2 = cos(Y) - 2 * sin(Y);
isequal(X, X2) % TRUE
It is not useful to optimize a code, which is a massive simplification of the real code. Calling DET() for a 2x2 matrix is a waste of time, because DET([a,b; c,d]) is simply: q*c - b*d. So either your problem can be solved in a cheap way without a loop, or you have hidden the important parts by posting an example which has been simplified too much.

5 commentaires

Bhromzara
Bhromzara le 29 Avr 2021
@Jan Yes, actually I have 16*16 matrix. Any suggestion for that size matrix?
Jan
Jan le 29 Avr 2021
Fine. This means that your example code has been simplified too much to show the actual problem. This happens frequently in forums. I suggest to edit the question and to add the relevant information there.
The natural extension would be to use the Symbolic Toolbox to generate the explicit formula for the determinant, after which you could matlabFunction up a vectorized function. However, the code for a 16 matrix would be prohibitively long, exceeding 2^53 terms.
Bruno Luong
Bruno Luong le 29 Avr 2021
Modifié(e) : Bruno Luong le 29 Avr 2021
No it does not exeeding 2^53 terms
>> log2(factorial(16))
ans =
44.2501
And using Laplace expansion is very bad idea, the cancellation between terms make it very bad numerical mehod. It can only used few very small dimension, let say <= 5.
Hmmm, I must have been misremembering.
I see it is 18! that is the last one before 2^53.
Let me see... If you do a number of up-front assignments along the lines of
AA = in(:,1); AB = in(:,2);
for each of the 256 different elements of a 16 x 16 matrix, then you can use two characters per entry (with 256 elements of 16 x 16 matrix, that is too many for one character variable names); and each term would be 2 characters occuring 16 times, plus 15 .* operators = 47 characters. There would be 16! = 20922789888000 terms, and one '+' character between each term so we get 16! * 48 - 1 = 1004293914623999 characters, plus the overhead of breaking out the inputs into variables. It works out to just under one petabyte.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by