TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Make Diagonal Matrix

Linear Algebra
Easy

Given a vector v∈RN\mathbf{v}\in\mathbb{R}^{N}v∈RN, construct an N×NN\times NN×N matrix whose main diagonal contains v\mathbf{v}v and whose remaining entries are zero:

Dij={vi,i=j0,i≠jD_{ij}=\begin{cases}v_i,&i=j\\0,&i\ne j\end{cases}Dij​={vi​,0,​i=ji=j​

Here, iii and jjj are row and column indices. Construct the matrix without np.diag and return it as a NumPy array.

Loading visualization...

Examples

Input: v = [3, 5]

Output: [[3, 0], [0, 5]]

Explanation: The two vector values occupy positions (0, 0) and (1, 1).

Input: v = [1.5]

Output: [[1.5]]

Input: v = [0, 0, 2]

Output: [[0, 0, 0], [0, 0, 0], [0, 0, 2]]

Hint 1

Initialize the output with np.zeros((values.size, values.size), dtype=values.dtype).

Hint 2

Assign with matrix[np.arange(values.size), np.arange(values.size)] = values.

Requirements

  • Create a square zero matrix with the same dtype as the input vector
  • Place each vector element at the matching row and column index
  • Do not use np.diag
  • Return a NumPy array of shape (N, N)

Constraints

  • v is a nonempty one-dimensional numeric list
  • Use NumPy only
Try Similar Problems
Matrix TraceMatrix TransposeMatrix InverseEigenvaluesMatrix Normalization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.
PrevNext

Make Diagonal Matrix

Linear Algebra
Easy

Given a vector v∈RN\mathbf{v}\in\mathbb{R}^{N}v∈RN, construct an N×NN\times NN×N matrix whose main diagonal contains v\mathbf{v}v and whose remaining entries are zero:

Dij={vi,i=j0,i≠jD_{ij}=\begin{cases}v_i,&i=j\\0,&i\ne j\end{cases}Dij​={vi​,0,​i=ji=j​

Here, iii and jjj are row and column indices. Construct the matrix without np.diag and return it as a NumPy array.

Loading visualization...

Examples

Input: v = [3, 5]

Output: [[3, 0], [0, 5]]

Explanation: The two vector values occupy positions (0, 0) and (1, 1).

Input: v = [1.5]

Output: [[1.5]]

Input: v = [0, 0, 2]

Output: [[0, 0, 0], [0, 0, 0], [0, 0, 2]]

Hint 1

Initialize the output with np.zeros((values.size, values.size), dtype=values.dtype).

Hint 2

Assign with matrix[np.arange(values.size), np.arange(values.size)] = values.

Requirements

  • Create a square zero matrix with the same dtype as the input vector
  • Place each vector element at the matching row and column index
  • Do not use np.diag
  • Return a NumPy array of shape (N, N)

Constraints

  • v is a nonempty one-dimensional numeric list
  • Use NumPy only
Try Similar Problems
Matrix TraceMatrix TransposeMatrix InverseEigenvaluesMatrix Normalization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.