필터 지우기
필터 지우기

i've a matrix of 6x4 and i want to count the rows how many times it occur in a matrix

조회 수: 3 (최근 30일)
let say the matrix is
A=[ 12 45 67 89
34 56 78 65
12 45 67 89
53 55 21 90
12 45 67 89
53 55 21 90 ]
i want the output to be as:
12 45 67 89 ----- 3 % because it occurs 3 times
34 56 78 65 ------1
53 55 21 90 ------2
plz help me

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 8일
A=[ 12 45 67 89
34 56 78 65
12 45 67 89
53 55 21 90
12 45 67 89
53 55 21 90 ]
[a,b,c]=unique(A,'rows','stable');
v=arrayfun(@(x) sum(c==x),1:size(a,1))';
out=[a v]
  댓글 수: 2
Emma Cairns
Emma Cairns 2017년 8월 9일
I have an instance where I need to do this twice with two separate matrices and I keep getting the same output for both using this code, is there any way I can make it work? Thanks!
Jan
Jan 2017년 8월 9일
편집: Jan 2017년 8월 9일
@Emma: Yes, there is a way. Of course there is. If you post the relevant part of your code (preferably as a new question and not by high-jacking this thread), we can even suggest a solution.
Andrei's method is slightly faster and (c)leaner.

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2013년 5월 8일
A=[ 12 45 67 89
34 56 78 65
12 45 67 89
53 55 21 90
12 45 67 89
53 55 21 90 ];
[a,c,c] = unique(A,'rows');
out = [a, histc(c,1:max(c))];

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by