generate an array according to some sequence
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Walter Roberson
on 10 Apr 2011
[v,idx] = sort(A);
T = triu(bsxfun(@lt,idx.',idx),1);
B = T(logical(triu(ones(length(A)),1))).';
3 Comments
Walter Roberson
on 10 Apr 2011
The T(logical...) part returns a column, and then the .' changes that to a row.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!