DunnoHowToCode
DunnoHowToCode

Reputation: 541

Choose 2 elements from 1D array to multidimensional array

I am currently doing some feature selection for my project and I have met some problem with creating an array of this sort.

I have an array of 115 rows and 1 column.

1
2
...
115

I would like to create a 2D array that takes in the first value and all the other values as the 2nd column, while not repeating any previous pair of value(1 2) is the same as (2 1)

1 2
1 3
...
1 115
2 3
2 4
...
114 115

My current code is not creating the array as intended. test is the array of 115 rows. test1 is the new array I want to create.

for i=1:115
    for j=i:115
        if (i == j)
            j=j+1;
        else
            test1(i,j)=test(j);
        end
    end
end

Any advice will be appreciated. Thank you!

Upvotes: 1

Views: 36

Answers (1)

rahnema1
rahnema1

Reputation: 15837

You can use nchoosek:

nchoosek(1:115,2)

Upvotes: 2

Related Questions