Sine wave

1 次查看(过去 30 天)
simira atraqi
simira atraqi 2012-4-16
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

回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by