Borrar filtros
Borrar filtros

Sine wave

3 visualizaciones (últimos 30 días)
simira atraqi
simira atraqi el 16 de Abr. de 2012
Comentada: Walter Roberson el 16 de Feb. de 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

Respuestas (1)

Walter Roberson
Walter Roberson el 16 de Abr. de 2012
All of your rows are the same, so consider using repmat()
  2 comentarios
simira atraqi
simira atraqi el 16 de Abr. de 2012
I am sorry but I didn't get it. What you mean?
Walter Roberson
Walter Roberson el 16 de Feb. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by