Sine wave

3 vues (au cours des 30 derniers jours)
simira atraqi
simira atraqi le 16 Avr 2012
Commenté : Walter Roberson le 16 Fév 2023
Hello Everyone,
With these codes, the matrix A have 4000 points and this take a long time, how can I let this program work quickly with less Matrix and the same wavelength 100 um?
Thanks
xRes = 1; % Sets the resolution (step length), in µm, along the x-axis.
yRes = 1; % Sets the resolution (step length), in µm, along the y-axis.
zRes = 1; % Sets the resolution (step length), in µm, along the z-axis.
xLength = 4000; % Sets the length of the surface in X.
yLength = 3000; % Sets the length of the surface in Y.
wavelength = 100;
f = @(x) 3*sin(x*2*pi/wavelength) + 1003; % The function for the sine wave with the
% amplitude 3 µm and wave length of about 100 µm. Mean level is
% 1003 µm.
% Prints the sine wave into a matrix:
A = zeros(yLength/yRes+1,xLength/xRes+1);
for k = 1:length(A(:,1))
for l = 1:length(A(1,:))
A(k,l) = f(l-1);
end
end

Réponses (1)

Walter Roberson
Walter Roberson le 16 Avr 2012
All of your rows are the same, so consider using repmat()
  2 commentaires
simira atraqi
simira atraqi le 16 Avr 2012
I am sorry but I didn't get it. What you mean?
Walter Roberson
Walter Roberson le 16 Fév 2023
for k = 1:length(A(:,1))
for l = 1:length(A(1,:))
A(k,l) = f(l-1);
end
end
You are not changing f within the loops, so A(1,5) = f(4) and A(2,5) = f(4) and A(3,5) = f(4) and so on -- each A(k,l) is exactly the same as A(k-1,l) . Therefore you only need to build A(1,:) and then you copy the row as many times as needed.
for l = 1 : size(A,2)
A(1,l) = f(l-1);
end
A = repmat(A(1,:), size(A,1), 1); %replicate the row

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by