MATLAB: Is this the correct syntax for replacing a specific element in a dependent vector

arraymatrixvector

numElem = 9990;
w = linspace(2*pi,2*pi*10^18,numElem);
wRes = 2*pi*10^9;
for n = 1:numElem
if w(n) > wRes
w = [w(1:n), wRes, w(n:numElem)];
end
end
freq = w ./ (2*pi);
lambda = 299792458 ./ freq;
if w == wRes
B = 2*pi./lambda;
else
B = pi/2 .*w./wRes;
end
I am trying to create a range of w's and make sure that a specific value, wRes is in there. For that specific wRes, I want an exceptional value for B. Is the above code the correct way of going about it?

Best Answer

Note that if you are trying to replace values > wRes with wRes, then another way of doing that is:
w = min(w, wRes);
Related Question