How can I create a new matrix which stores the diagonals, but it first starts with a matrix full of zeros?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
%A
rng('default')
r2 = randi(100,3,3); % Creating a 3x3 matrix, with random values from 1 to 100
%B
d = diag(r2) % Obtain the diagonal elements
%C
Metod1 = d(2,1) % Using two parameters (row 2, column 1)
Metod2 = d(3) % Using one parametr (3rd element)
How can I create a new matrix (3x3) which stores the diagonals, but it first starts with a matrix full of zeros?
1 Commento
Risposta accettata
Chunru
il 4 Mag 2022
% start with zero matrix
n=3;
a = zeros(3)
% random entries along diagonal
a(1:n+1:n*n) = randi(100, [n 1])
% alternatively
a = diag(randi(100,[n,1]))
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!