Generate a Matrix with the structure in the description

1 view (last 30 days)
Hi everyone i'm new
i am trying to write an algorithm to generate a matrix which has the following structure
(modified)
1,2
1,3
1,4
.
.
.
and so on till
1,128
then
2,3
2,4
2,5
.
.
an so on till
2,128
and so on till
127,128
after that i wil have to convert it to binary . for it i will use the in bulit de2bi function.
so i have to generate a 2x2 matrix
where the first column starts with 1 and ends with 128 and the second column starts with 2 and repeats everytime 128 is reached.
any suggestion , resources may be of great help.
the only i came up with is this
r1=[1:2]; %for the rows with 1 and inscreasing values of ii
r=[r1];
for ii=1:126
r0=[1:ii+1:ii+2];
r=[r;{r0}];
end

Accepted Answer

Bruno Luong
Bruno Luong on 5 Dec 2020
A=nchoosek(1:128,2)

More Answers (1)

Rik
Rik on 4 Dec 2020
No loops required:
X=tril(ones(128));
[r,c]=find(X);
out=[c r];
For the next step I would suggest you create a cell array with the binary of 1:128, which you can then index. That will prevent a lot of repeated conversions.
  1 Comment
Fandeu Tchiaga Brice Arsene
thank you very much this greatly helped .
i am currently working on removing these repetitions cause the final matrix should have 8255 rows.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!