Borrar filtros
Borrar filtros

How can I generate n*(n^2) matrix with n 1's per row, indented n times?

2 visualizaciones (últimos 30 días)
Hi,
I'm trying to generate the following matrix (n=3 in this case)
A1partition =
1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1
or for example if n=4:
A1partition =
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
But I'm struggling to indent the 1's each row. This is my code as now
n=3;
A = zeros(n,n*n);
for i = 1:n
for j=1:n
A(i,j)=1;
end
end
A
which gives
A =
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0

Respuesta aceptada

KSSV
KSSV el 15 de Mzo. de 2022
n = 4 ;
A = zeros(n,n,n) ;
for i = 1:n
A(i,:,i) = ones(n,1) ;
end
A = reshape(permute(A,[1,2,3]),size(A,2),[])
A = 4×16
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by