Generate a Matrix with the structure in the description

1 visualización (últimos 30 días)
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

Respuesta aceptada

Bruno Luong
Bruno Luong el 5 de Dic. de 2020
A=nchoosek(1:128,2)
  1 comentario
Fandeu Tchiaga Brice Arsene
Fandeu Tchiaga Brice Arsene el 5 de Dic. de 2020
This is even Quicker. very grateful for that.
thanks for the quick replies guys
top.

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 4 de Dic. de 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 comentario
Fandeu Tchiaga Brice Arsene
Fandeu Tchiaga Brice Arsene el 5 de Dic. de 2020
thank you very much this greatly helped .
i am currently working on removing these repetitions cause the final matrix should have 8255 rows.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by