필터 지우기
필터 지우기

How to count repetition of a row in a matrix?

조회 수: 4 (최근 30일)
Mr M.
Mr M. 2018년 11월 13일
댓글: Mr M. 2018년 11월 13일
I have a matrix: M = [1,2; 2,3; 1,5; 2,3; 1,4; 1,6; 2;3, 8,9]; And I want to check number of occurance of a given row, for example [2,3]. How to do that without for cycle?

채택된 답변

Stephen23
Stephen23 2018년 11월 13일
편집: Stephen23 2018년 11월 13일
>> M = [1,2; 2,3; 1,5; 2,3; 1,4; 1,6; 2,3; 8,9];
>> R = [2,3];
Method one: eq: and all:
>> nnz(all(M==R,2))
ans = 3
For MATLAB versions before R2016b use bsxfun and eq:
>> nnz(all(bsxfun(@eq,M,R),2))
ans = 3
Method two: ismember:
>> nnz(ismember(M,R,'rows'))
ans = 3
  댓글 수: 1
Mr M.
Mr M. 2018년 11월 13일
sum(ismember(M,R,'rows')) is the same?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by