Separating a list of numbers by their first few digits?
1 view (last 30 days)
Show older comments
I have a list that has some numbers that start with similar digits, and I want to separate the list by their first couple digits.
Ex. [3345 5543 3346 3347 5555 5567 3355 5532]
And I need a vector of values starting with 33 [3345 3346 3347 3355] and a vector of values starting with 55 [5555 5543 5567 5532].
0 Comments
Answers (2)
KSSV
on 7 Mar 2018
d = [3345 5543 3346 3347 5555 5567 3355 5532] ;
digits = dec2base(d,10) - '0' ;
[c,ia,ib] = unique(digits(:,1:2),'rows') ;
iwant = cell(size(c,1),1) ;
for i = 1:size(c,1)
iwant{i} = d(ib==i) ;
iwant{i}
end
0 Comments
Star Strider
on 7 Mar 2018
V = [3345 5543 3346 3347 5555 5567 3355 5532];
Vu = unique(fix(V/100)); % Divide By 100 To Define Categories
[idx,edges] = discretize(V,numel(Vu)); % ‘discretize’ Introduced in R2015a
Out = accumarray(idx(:), V(:), [], @(x){x}); % Desired Result
Result = [Out{:}] % Display Result
Result =
3355 5567
3346 5555
3347 5532
3345 5543
0 Comments
See Also
Categories
Find more on Language Fundamentals 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!