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

1 view (last 30 days)
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

Accepted Answer

dpb
dpb on 3 Oct 2022
Edited: dpb on 3 Oct 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.

More Answers (1)

Kevin Holly
Kevin Holly on 3 Oct 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')

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by