필터 지우기
필터 지우기

generate an array according to some sequence

조회 수: 1 (최근 30일)
Chien-Chia Huang
Chien-Chia Huang 2011년 4월 10일
Suppose I have an array [4 2 1 3] (length = 4) and I want to generate another array of length 6(= 0.5*4*(4-1)).
The vlaues of the new array represents the relationship between i and j, for i < j.
If i precedes j than (i,j) = 1; and 0, otherwise.
That is [(1,2) (1,3) (1,4) (2,3) (2,4) (3,4)] = [0 1 0 1 0 0].
Is there any way out to evade using loops?
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 2011년 4월 10일
[v,idx] = sort(A);
T = triu(bsxfun(@lt,idx.',idx),1);
B = T(logical(triu(ones(length(A)),1))).';
  댓글 수: 3
Chien-Chia Huang
Chien-Chia Huang 2011년 4월 10일
One more note on Walter's solution.
B is built in a column-wise fashion.
B = [(1,2) (1,3) (2,3) (1,4) (2,4)....].
Walter Roberson
Walter Roberson 2011년 4월 10일
The T(logical...) part returns a column, and then the .' changes that to a row.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by