combine elements of a large vector in matlab

I need to take pairwise combination of a large vector v (770x1). The commands combnk and combntns come with a precaution that they are slow for large vectors.
>> tic, a = combnk(v,2); toc
Elapsed time is 3.081594 seconds.
>> tic, a = combntns(v,2); toc
Elapsed time is 3.147092 seconds.
Is there any other alternative that is more efficient?

 채택된 답변

Matt Fig
Matt Fig 2012년 11월 22일
편집: Matt Fig 2012년 11월 22일

1 개 추천

>> tic, A = nchoosek(1:770,2); toc
Elapsed time is 2.286846 seconds.
>> tic, B = combnk(1:770,2); toc
Elapsed time is 2.299141 seconds.
>> tic, C = combinator(770,2,'c','no'); toc
Elapsed time is 0.019995 seconds.
>> isequal(sortrows(A),sortrows(B),sortrows(C))
ans =
1
COMBINATOR is found here: File Exchange File.

댓글 수: 3

Swagatika
Swagatika 2012년 11월 22일
thanks. That is really fast. But my problem is a bit different from this...the vector is not from 1 to 770, it has different elements which I need to combine, not exactly 1:770.
Matt Fig
Matt Fig 2012년 11월 22일
편집: Matt Fig 2012년 11월 22일
I bet you didn't bother to read the help yet. If you have another vector than 1:770, simply use the output from COMBINATOR as an index into your vector.
>> A = [9 8 7];
>> A(combinator(length(A),2,'c','n'))
ans =
9 8
9 7
8 7
Also, if your question has been answered, please accept the answer. Thanks.
Swagatika
Swagatika 2012년 11월 22일
thanks. Just figured that out :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2012년 11월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by