I would like to plot few values of a vector based on threshold setting from another vector.

1 Ansicht (letzte 30 Tage)
clear all;
close all;
x=[1 2 3 4 5 6 7 8 9 10];
y=[1 2 3 4 5 6 7 8 9 10];
figure ()
plot(x(y>3 & y<5),y(y>3 & y<5))
if i will write plot(x(y>3 ),y(y>3)) it works, but I can not make it work for two different thresholds

Akzeptierte Antwort

dpb
dpb am 3 Okt. 2022
Bearbeitet: dpb am 3 Okt. 2022
It works; you just can't see the single point that is left because the single point on a default line width without a marker just is too small/light to be able to see...
x=[1 2 3 4 5 6 7 8 9 10];
y=[1 2 3 4 5 6 7 8 9 10];
subplot(2,1,1)
plot(x(y>3 & y<5),y(y>3 & y<5),'kx-')
xlim([0 10])
subplot(2,1,2)
plot(x,y,'kx-')
hold on
lo=3; hi=8; % set different range; use variables instead of burying magic numbers in code..
plot(x(y>lo & y<hi),y(y>lo & y<hi),'r-') % now will show up with default line width
xlim([0 10])
ADDENDUM
Here is place I like my "syntactical sugar" routine iswithin to reduce clutter in the user code by moving the comparisons to a lower level -- using it the above could be written as
lo=4;hi=4; % duplicate original, iswithin is inclusive
ix=iswithin(x,lo,hi)&iswithin(y,lo,hi); % logical addressing vector
plot(x(ix),y(ix),'kx') % plot original with a marker
where iswithin is
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
I keep a "Utilities" subdirectory as part of my MATLABPATH just behind my current working directory in order; such general-use functions such as this reside there so are always available.

Weitere Antworten (1)

Kevin Holly
Kevin Holly am 3 Okt. 2022
x=[1 2 3 4 5 6 7 8 9 10];
y=[1 2 3 4 5 6 7 8 9 10];
x(y>3 & y<5)
ans = 4
y(y>3 & y<5)
ans = 4
figure
plot(x(y>3 & y<5),y(y>3 & y<5),'r')
figure
scatter(x(y>3 & y<5),y(y>3 & y<5),'r')

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by