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

1 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2022-3-15
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by