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

1 回表示 (過去 30 日間)
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

採用された回答

dpb
dpb 2022 年 10 月 3 日
編集済み: dpb 2022 年 10 月 3 日
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.
  2 件のコメント
Panagiotis Bonas
Panagiotis Bonas 2022 年 10 月 3 日
You are right, Thank you a lot for your answer :)
dpb
dpb 2022 年 10 月 3 日
So, if it solves the problem, ACCEPT it so others can know if nothing else.

サインインしてコメントする。

その他の回答 (1 件)

Kevin Holly
Kevin Holly 2022 年 10 月 3 日
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')

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by