if function for array

9 ビュー (過去 30 日間)
Josefina Ottitsch
Josefina Ottitsch 2019 年 2 月 13 日
回答済み: dpb 2019 年 2 月 13 日
Hello I have a programm, that looks something like this:
x=[52.23;52.45;56.32;55.48;56.32;53.39;55.53;53.67;57.89]
plot(x)
and now i would like my program to draw the dots of numbers 52 till 54 and 54 till 56 and 56 till 58 in seperate graphs, how do i do this? i wanted to solve this by using if but it doesnt quite work.
thank you for the help!
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 2 月 13 日
to draw the dots of numbers 52 till 54, what does it mean?
Jan
Jan 2019 年 2 月 13 日
"solve this by using if but it doesnt quite work" - post the code and the error message. This is much better than letting the readers guiess, what you try and observe.

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

採用された回答

dpb
dpb 2019 年 2 月 13 日
I have a little utility function ("syntax sugar") for such things...
plot(x(iswithin(x,52,54)))
where
>> type iswithin.m
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);
>>

その他の回答 (1 件)

Jan
Jan 2019 年 2 月 13 日
x=[52.23;52.45;56.32;55.48;56.32;53.39;55.53;53.67;57.89]
subplot(2,2,1)
plot(x)
subplot(2,2,2)
index = find(52 < x && x <= 54);
plot(index, x(index));
subplot(2,2,3)
index = find(54 < x && x <= 56);
plot(index, x(index));
subplot(2,2,4)
index = find(56 < x && x <= 58);
plot(index, x(index));

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by