how to change the diagonal of a matrix 3x3 with a vector with 3 elements using for loop structurein matlab in javascript

Matlab Solution using for loop

main.m
% create a 3 x 3 matrix
A = magic(3);

% create a vector of 3 elements
v = [1 2 3];

% loop through the diagonal elements and replace them with elements from the vector
for i = 1:3
    A(i,i) = v(i);
end

% print the updated matrix
disp(A);
241 chars
14 lines

Javascript Solution using for loop

index.tsx
// create a 3 x 3 matrix
let A = [[1,2,3],[4,5,6],[7,8,9]];

// create a vector of 3 elements
let v = [1,2,3];

// loop through the diagonal elements and replace them with elements from the vector
for(let i=0; i<3; i++) {
  A[i][i] = v[i];
}

// print the updated matrix
console.log(A);
287 chars
14 lines

gistlibby LogSnag