How to extract values from an array?
27 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Rik
il 9 Mar 2022
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.
Risposta accettata
Rik
il 9 Mar 2022
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)
%remove the 30s
L=B==30;
B(L)=[];
N(L)=[];
%decode back to the normal array
new_array = RunLength(B, N)
0 Commenti
Più risposte (1)
David Hill
il 9 Mar 2022
a=num2str(yourArray==30);
a=a(a~=' ');
[s,f]=regexp(a,'[1]*');
new_array=[s,f];
Vedere anche
Categorie
Scopri di più su String Parsing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!