Why am I getting the error : Operands to the || and && operators must be convertible to logical scalar values for the below code?
1 view (last 30 days)
Show older comments
function yq = myInterp(X, Y, xq)
step = 1; stop = length(X);
for i = 1:step:stop;
%If xq is a value in the road generator, then use that number
if xq == X(i)
yq = Y(i);
break
%If xq is in between two numbers in the road generator, the use interpolation
elseif (xq > X(i) && xq < X(i+1))
yq = Y(i)+(Y(i+1)-Y(i))*(xq-X(i))/(X(i+1)-X(i));
break
end
end
0 Comments
Accepted Answer
Andrei Bobrov
on 29 Jan 2015
Edited: Andrei Bobrov
on 29 Jan 2015
X = [0 2 5 20 40]';
xq = 40*rand(20,1);
xq(3)= 1.3;
xq(5) = 5;
Y = [2 5 8 40 60]';
function yq = myInterp(X, Y, xq) % without loop
dyx = diff(Y)./diff(X);
[~,c] = histc(xq,X);
yq = Y(c) + dyx(c).*(xq - X(c));
end
function yq = myInterp(X, Y, xq) % with loop
n = length(X);
yq = zeros(size(xq));
for ii = 1:n-1
t = xq >= X(ii) & xq < X(ii+1); % '&' for array, '&&' for scalar
yq(t) = Y(ii)+(Y(ii+1)-Y(ii))*(xq(t)-X(ii))/(X(ii+1)-X(ii));
end
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!