How to extract values from an array?

Hello all,
I have an array with values that repeat , is it possible to determine the start and end points of a particular value in the array. For example
array = [10 10 10 30 30 30 30 30 4 4 4 4 4 30 30 30 30 30 30 2 2 2 2 2 30 30 30 30];
the goal is to extract those values in between the 30s.
How can i go about this? Thanks

4 件のコメント

Rik
Rik 2022 年 3 月 9 日
What would be the intended output? Perhaps this?
new_array=[4 2];
You can probably use most of the RunLength function Jan published on the file exchange.
Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 9 日
let me check out the RunLength function. i want to use the indices of the start and end 30 to extract the values in between to a new array. Thanks
Rik
Rik 2022 年 3 月 9 日
If you want to extract the numbers, do you mean you want [4 2], or [4 4 4 4 4 2 2 2 2 2];? (or something else)
That is also probably important information if David's answer doesn't work for you.
Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 9 日
Yes you are right, I would want as output [4 4 4 4 4 2 2 2 2 2].

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

 採用された回答

Rik
Rik 2022 年 3 月 9 日

0 投票

Using RunLength by Jan:
array = [10 10 10 30 30 30 30 30 4 4 4 4 4 30 30 30 30 30 30 2 2 2 2 2 30 30 30 30];
%first select only the parts between the first and last 30
new_array=array(find(array==30,1,'first'):find(array==30,1,'last'));
[B, N] = RunLength(new_array)
B = 1×5
30 4 30 2 30
N = 5×1
5 5 6 5 4
%remove the 30s
L=B==30;
B(L)=[];
N(L)=[];
%decode back to the normal array
new_array = RunLength(B, N)
new_array = 1×10
4 4 4 4 4 2 2 2 2 2

その他の回答 (1 件)

David Hill
David Hill 2022 年 3 月 9 日

0 投票

a=num2str(yourArray==30);
a=a(a~=' ');
[s,f]=regexp(a,'[1]*');
new_array=[s,f];

1 件のコメント

Daniel Adeniyi
Daniel Adeniyi 2022 年 3 月 9 日
Is it possible to have an output such as [4 4 4 4 4 2 2 2 2 2] instead of their unique value.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2022 年 3 月 9 日

回答済み:

Rik
2022 年 3 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by