Grouping elements by conditions
Show older comments
I have an array [ 1 1.2 1.5 1.9 5 8 8.1 10 12 12.3 12.5]. I'm trying to group the elements that are within a window of length 1, so I get [ 1 1.2 1.5 1.9] [8 8.1] [12.3 12.5] How can this be done efficiently
2 Comments
Matt J
on 21 Aug 2018
Will the grouped elements always be consecutive?
dani elias
on 19 Nov 2020
how can I use the same concept to group every 4 binary digit, for the case I have an array of 256 binary number,
example
a=[1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0];
to be in a form of
b=[1111;1111;1011;1000]
Accepted Answer
More Answers (1)
Yuvaraj Venkataswamy
on 21 Aug 2018
Edited: Matt J
on 21 Aug 2018
This is your answer.
X=[ 1 1.2 1.5 1.9 5 8 8.1 10 12 12.3 12.5];
Len=diff([0,find(diff(a)>1),numel(a)]);
S=mat2cell(X,1,Len);
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!