Given a vector v∈RN, construct an N×N matrix whose main diagonal contains v and whose remaining entries are zero:
Dij={vi,0,i=ji=jHere, i and j are row and column indices. Construct the matrix without np.diag and return it as a NumPy array.
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]]
Initialize the output with np.zeros((values.size, values.size), dtype=values.dtype).
Assign with matrix[np.arange(values.size), np.arange(values.size)] = values.
Sign in to take notes on this problem
Accepts: array
Given a vector v∈RN, construct an N×N matrix whose main diagonal contains v and whose remaining entries are zero:
Dij={vi,0,i=ji=jHere, i and j are row and column indices. Construct the matrix without np.diag and return it as a NumPy array.
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]]
Initialize the output with np.zeros((values.size, values.size), dtype=values.dtype).
Assign with matrix[np.arange(values.size), np.arange(values.size)] = values.
Sign in to take notes on this problem
Accepts: array