Sine wave
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Walter Roberson
on 16 Apr 2012
All of your rows are the same, so consider using repmat()
2 Comments
Walter Roberson
on 16 Feb 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
See Also
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!