Sine wave

조회 수: 3 (최근 30일)
simira atraqi
simira atraqi 2012년 4월 16일
댓글: Walter Roberson 2023년 2월 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에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by