I am trying to accomplish this same task using the find() command but I'm not sure how.

1 view (last 30 days)
clear all
n = 1
for k = 0:.1:6
x(n) = k
if (2 <= x(n))&&(x(n) <= 4)
y(n) = 2*k
else
y(n) = -5*k
end
n=n+1
end
plot(x,y)

Accepted Answer

Star Strider
Star Strider on 23 Apr 2019
Here is a version that uses a logical vector in place of the find function:
x = 0:0.1:6;
y = -5*x;
lv = (2 <= x) & (x <= 4); % Logical Vector Of ‘x’ Elements Satisfying Conditions
y(lv) = 2*x(lv ~= 0);
figure
plot(x,y)
A version using the find function would be:
x = 0:0.1:6;
y = -5*x;
idx = find((2 <= x) & (x <= 4)); % Index Vector Of ‘x’ Elements Satisfying Conditions
y(idx) = 2*x(idx);
figure
plot(x,y)

More Answers (1)

Walter Roberson
Walter Roberson on 23 Apr 2019
Hint:
t = 1:1.5:20;
output = zeros(size(t));
ind = find(t >= 3 & t <= 9);
output(ind) = sin(t(ind));
ind = setdiff(1:numel(t), ind);
output(ind) = -cos(t(ind));

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!