필터 지우기
필터 지우기

How to easily detect all the equal rows of an array with saving the indices of the elements?

조회 수: 3 (최근 30일)
I have a matrix as shown here
A=[ -1 0 0;
-1 0 0;
-1 0 0;
-1 0 0;
1 0 0;
1 0 0;
] I want to detect all the equal rows in the matrix and build another matrix with the indices of the equal rows. For exemple here row1=rows2=row3=row4 and row5=row6... I want to have a matrix like this [1 2;1 3;1 4; 5 6]. I find a solution but it uses a lot of for loops so it's not fast enough when I apply it to a large matrix. Is there any easiest solution for this. Thank you.

답변 (1개)

Ahmet Cecen
Ahmet Cecen 2016년 5월 19일
This does all of that:
[C,ia,ic] = unique(A,'rows')
  댓글 수: 2
Sleh Eddine Brika
Sleh Eddine Brika 2016년 5월 19일
Thank you Ahmet, I had already tried this function, but its outputs requires additional treatments to get the result that I want. For my exemple, we get ia =[1;5] ic = [1 ;1;1;1;2;2 ] so we need to count for each element of ic how many times it's repeated and then create another matrix that take the first element of ia in column1 and according to the number of repetition in ic (let's say n) affect index for ia(1)+1 to n for the column2. It's a solution but I am working with matrices with more then 2000 rows so it's not fast enough.

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

카테고리

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