Adugna
Adugna

Reputation: 33

Generate vectors using a pattern of numbers

I want to generate two vectors of equal length (n) from numbers that follow a specific pattern for each vector. For n=10 the vectors would be

  V1 = [2,3,3,4,4,4,5,5,5,5] and V2 = [1,1,2,1,2,3,1,2,3,4]

For very large n How can I automate the generation of these vectors by following the same pattern.

Any suggestion is appreciated.

Upvotes: 1

Views: 55

Answers (1)

Divakar
Divakar

Reputation: 221514

You are basically looking for row and column indices of upper/lower triangular matrix. So, we can use find and triu -

[v2,v1] = find(triu(true(5),1))

Sample run -

>> [v2,v1] = find(triu(true(5),1));
>> v1.'
ans =
     2     3     3     4     4     4     5     5     5     5
>> v2.'
ans =
     1     1     2     1     2     3     1     2     3     4

We can also bsxfun to create the upper triangular matrix, like so -

bsxfun(@lt,(1:5)',1:5)

Upvotes: 3

Related Questions