Unique values in an array, with specific digits
Mostra commenti meno recenti
Hi, I have an array of 7 digit numbers. i want to count the number of occurrences of unique values starting with two digits, say, 12*****. How can I do that? What about if I want to calculate the frequency of unique values ending with two specific digits, say, *****12?
To calculate unique occurrences for all values, the following works:
a = unique(input)
output = [a,histc(input(:),a)]
Any suggestions would be appreciated! Thanks!
Sushma
Risposta accettata
Più risposte (1)
Sean de Wolski
il 27 Set 2016
Modificato: Sean de Wolski
il 27 Set 2016
Starting in R2016b this is much easier with the string class.
x = [1234; 1256; 1334]
s = string(x)
unique(extractBefore(s,3)) % First two
unique(reverse(extractBefore(reverse(s),3))) % Last two
In older releases:
% First two
unique(cellfun(@(x)x(1:2),cellstr(num2str(x)),'uniformoutput',false))
% Last two
unique(cellfun(@(x)x(end-1:end),cellstr(num2str(x)),'uniformoutput',false))
Categorie
Scopri di più su Entering Commands in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!