Separating a list of numbers by their first few digits?

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].

Réponses (2)

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
Using the discretize (link) function
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by