Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
Ingrid Wilson
Ingrid Wilson le 15 Mar 2022
Commenté : Ingrid Wilson le 15 Mar 2022
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

Réponse acceptée

KSSV
KSSV le 15 Mar 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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by