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

1 vue (au cours des 30 derniers jours)
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)

Réponse acceptée

Star Strider
Star Strider le 23 Avr 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)

Plus de réponses (1)

Walter Roberson
Walter Roberson le 23 Avr 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));

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by