Replacing part of an array with another

How do I replace the diagonal of one 2d array with the diagonal of another? The arrays have the same size.

 Accepted Answer

KSSV
KSSV on 8 Nov 2022
Edited: KSSV on 8 Nov 2022
A = rand(4) ;
B = rand(4) ;
n = size(A,1);
A(1:(n+1):end) = diag(B) ; % repalce diagonal elements of A with diagonal elements of B

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 8 Nov 2022

Edited:

on 8 Nov 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!