How to group arrays in matrix

조회 수: 5 (최근 30일)
Amy Xu
Amy Xu 2017년 4월 10일
댓글: Stephen23 2017년 4월 10일
Matrix A as follows:
A = [
1
1
1
2
2
4
4
7
8
8
9
];
I want to group by matrix A as follows:
B = [
1 3
2 2
4 2
7 1
8 2
9 1
];
  댓글 수: 1
Stephen23
Stephen23 2017년 4월 10일
The old fashioned way:
>> U = unique(A);
>> [U,hist(A,U).']
ans =
1 3
2 2
4 2
7 1
8 2
9 1

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

답변 (2개)

Thorsten
Thorsten 2017년 4월 10일
편집: Thorsten 2017년 4월 10일
If the second row is the number of occurrences then you can use
[a, ~, c] = unique(A);
B = [a, accumarray(c, 1)];

Guillaume
Guillaume 2017년 4월 10일
A = [1 1 1 2 2 4 4 7 8 8 9]'
B = unique(A);
B = [B, histcounts(A, [B; Inf]).']

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by