필터 지우기
필터 지우기

Find the number of unique rows and its corresponding index in a matrix

조회 수: 2 (최근 30일)
Hi there, I need to list the unique rows and its corresponding index in a matrix without the using the unique function. I have been suggested to use the unique function but it seems rather time consuming and I have to run many iterations. Any help will be appreciated. Cheers, Vishal
Data:
v1=[1 2 3 4 5; 5 3 3 2 1; 1 2 4 9 7; 1 2 3 4 5; 5 3 3 2 1]
[a,b,c]=unique(v1,'rows','stable')
The required output which I get from the unique function is:
a =
1 2 3 4 5
5 3 3 2 1
1 2 4 9 7
b =
1
2
3
c =
1
2
3
1
2
  댓글 수: 7
dpb
dpb 2013년 8월 27일
편집: dpb 2013년 8월 27일
Well, it's a big job from scratch every time.
Is this simulation the selection of a subset each time that I seem to recall something about in an earlier posting? If so, I'd suggest the solution may be to do it once for the complete set and the use that and select from it for each randomized sample as well as from the data. That is if the data itself aren't changing, only the sample space.
Jing
Jing 2013년 8월 28일
How long does it take? I don't think UNIQUE will take a lot of time...and my test is running 20 times the UNIQUE line with your v1, the running time is always less than 0.005 seconds.

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

채택된 답변

Jan
Jan 2013년 8월 28일
편집: Jan 2013년 8월 28일
Perhaps this is more efficient:
v1 = [1 2 3 4 5; 5 3 3 2 1; 1 2 4 9 7; 1 2 3 4 5; 5 3 3 2 1];
vX = v1 * [1, 11, 121, 1331, 14641];
[dummy, b, c] = unique(vX, 'stable');
a = v1(b, :);
unique() has a remarkable overhead, so you could create a local copy of it and remove the not required checks of the inputs etc.
  댓글 수: 1
Vishal
Vishal 2013년 8월 29일
Cheers mate. Shaved off 5% of the computing time. Small typo in line 2
v1 = [1 2 3 4 5; 5 3 3 2 1; 1 2 4 9 7; 1 2 3 4 5; 5 3 3 2 1]; vX = v1 * [1; 11; 121; 1331; 14641]; [dummy, b, c] = unique(vX, 'stable'); a = v1(b, :);

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by