About Preallocating for speed

조회 수: 1 (최근 30일)
Hari
Hari 2017년 2월 14일
답변: Alexandra Harkai 2017년 2월 14일
What is a more efficient way of writing this code?
NumberedOD = [];
for a = 1:(N-1)
for b = (a+1) :N
NumberedOD = [NumberedOD;a, b];
end
end
My aim is to get a list of numbers like this:
1 2
1 3
.
.
1 N
2 3
2 4
.
.
  댓글 수: 1
Adam
Adam 2017년 2월 14일
편집: Adam 2017년 2월 14일
Purely from a pre-allocation perspective you can work out the number of elements there will be in that matrix - it is fixed and calculable so you can preallocated a zeros matrix. I'm sure there is a quicker vectorized way to do it though, I just don't have time to think about it myself right now. I'm sure someone will suggest one though.

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

채택된 답변

Alexandra Harkai
Alexandra Harkai 2017년 2월 14일
NumberedOD = nchoosek(1:N, 2);

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by