Unique values in an array, with specific digits

조회 수: 3 (최근 30일)
sushma sharma
sushma sharma 2016년 9월 27일
댓글: Sean de Wolski 2016년 9월 29일
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

채택된 답변

sushma sharma
sushma sharma 2016년 9월 27일
close, but not quite. while it lists the unique combinations of first and last digits, how can i get the frequency of each? e.g. count of numbers that begin with the digits 12 (12*****) and count of numbers ending with the digits 12 (e.g. *****12). any help would be appreciated! thank you!
sushma
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2016년 9월 29일
You have to use all of the outputs from unique
[uv,~,idx] = unique(...)
n = accumarray(idx,1)
uv n

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2016년 9월 27일
편집: Sean de Wolski 2016년 9월 27일
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))

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by