-1

I want to create a matrix where The matrix is diagonal elements of the matrix elements other? without loop in Matlab

A=[1 2;3 4]
B=[1 0 0 0;0 2 0 0;0 0 3 0;0 0 0 4]
abbas
  • 57
  • 6

1 Answers1

0

In Matlab case, just do the following

v = A'(:) % transpose since it makes long column by concat left to right column
B = diag(v)

In Python case, you can use numpy

import numpy as np
A = np.array([[1,2], [3,4]])
B = np.diag(A.ravel())
titipata
  • 5,321
  • 3
  • 35
  • 59