필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Ingrid Wilson
Ingrid Wilson 2022년 3월 15일
댓글: Ingrid Wilson 2022년 3월 15일
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