How to group specific values i several vectors following a condition?

5 ビュー (過去 30 日間)
e_frog
e_frog 2021 年 10 月 25 日
コメント済み: Mathieu NOE 2021 年 10 月 25 日
Hi,
I am struggeling to find a method to extract values that comply with a specific condition from a vector.
The input is a random vector like x = [3,5,13,43,54,3,2,3,5,6,3,18,19,17,25.2,5,3,5,3,59,0,4,3];
I would like to extract the 'blocks' of values, that are e.g. greater than the value of 10.
The expected result would be: res1 = [13,54], res2=[18,19,17,25.2] and res3=[59]
The number of result vectors is not predetermined, as x is a random vector. The result would not have to be extracted as vectors, cells would be fine as well.
Thanks in advance!

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 10 月 25 日
hello
IMHO, according to the rules , the result should be : res1 = [13,43,54], res2=[18,19,17,25.2] and res3=[59]
and this is the code that achieve it :
x = [3,5,13,43,54,3,2,3,5,6,3,18,19,17,25.2,5,3,5,3,59,0,4,3];
%% main code
ind = (x>10);
p=find(diff(ind)~=0);
nb_of_blocks = length(p)/2;
for ci = 1:nb_of_blocks
ii = (1:2)+(ci-1)*2;
block{ci} = x(p(ii(1))+1:p(ii(2)));
end
  2 件のコメント
e_frog
e_frog 2021 年 10 月 25 日
編集済み: e_frog 2021 年 10 月 25 日
You are right, res3=[0] is a typo. I edited the question accordingly.
Your answer works exactly the way needed. Thanks so much!
Mathieu NOE
Mathieu NOE 2021 年 10 月 25 日
My pleasure !

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by