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

1 view (last 30 days)
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

Accepted Answer

KSSV
KSSV on 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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!