How to select a value from a vector that satisfies certain conditions

6 ビュー (過去 30 日間)
Rouyu Chen
Rouyu Chen 2023 年 4 月 7 日
コメント済み: Rouyu Chen 2023 年 4 月 7 日
Dear experts,
I have two 15*1 column vectors:
one represents a lower bound [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5], and
another represents an upper bound [2 3 4 5 6 2 3 4 5 6 2 3 4 5 6]
and there is another vecor [1 2 3 4 5]
I then want to generate a column vector which each element is a value from the thrid vector that greater or equal to the lower bound and smaller than upperbound, i.e. from the above example I want to get a result of [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
I was wondering what code can help me do this?
Thank you so much!

採用された回答

Vilém Frynta
Vilém Frynta 2023 年 4 月 7 日
% Your vectors
lower_bound = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5];
upper_bound = [2 3 4 5 6 2 3 4 5 6 2 3 4 5 6];
vec = [1 2 3 4 5];
% Make 'vec' same length
vec = repmat(vec,[1 3])
vec = 1×15
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
% Apply conditions
result = vec(vec >= lower_bound & vec < upper_bound)
result = 1×15
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Maybe like this? :-)
It definitely could get more complex depending on what other vectors you are expecting.
  1 件のコメント
Rouyu Chen
Rouyu Chen 2023 年 4 月 7 日
It works, Thank you so much!
I was wondering if there is any other way to solve this, for example, when we cannot repeat the vector to make the length same as the lower bound and upper bound:
Lower_bound=[1 2 2 3 4 5 5]
Upper_bound=[2 3 3 4 5 6 6]
vec=[1 2 3 4 5]
Thanks again!

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by