How to replace elements of a matrix with the scaled identity?

조회 수: 1 (최근 30일)
Shikhar Shah
Shikhar Shah 2019년 12월 9일
댓글: Shikhar Shah 2019년 12월 9일
I have some arbitrary square matrix, for example:
A = [ 1 2 3; 4 5 6; 7 8 9 ];
I would like to replace each element with its scalar value multiplied by the identity of some size n. For example, taking n=2, I want to get:
B = [ 1 0 2 0 3 0;
0 1 0 2 0 3;
4 0 5 0 6 0;
0 4 0 5 0 6;
7 0 8 0 9 0;
0 7 0 8 0 9 ];
Since the size n is constant, the matrix B is guaranteed to have consistent dimensions. Is there a quick way to do this without having to loop over the elements of A and find the corresponding indices of B to place the identity?

채택된 답변

Matt J
Matt J 2019년 12월 9일
B=kron(A,eye(n))
  댓글 수: 1
Shikhar Shah
Shikhar Shah 2019년 12월 9일
This makes a lot of sense. It is useful that I can use any matrix for the second argument of kron(). Thank you so much for your quick and simple answer!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by