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

1 次查看(过去 30 天)
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)

采纳的回答

Star Strider
Star Strider 2019-4-23
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)

更多回答(1 个)

Walter Roberson
Walter Roberson 2019-4-23
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));

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by