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

1 visualización (últimos 30 días)
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)

Respuesta aceptada

Star Strider
Star Strider el 23 de Abr. de 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)

Más respuestas (1)

Walter Roberson
Walter Roberson el 23 de Abr. de 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));

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by