Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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)

Akzeptierte Antwort

Star Strider
Star Strider am 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)

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 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));

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by