People
Companies
Institutions
Jobs
Q&A
News
AI Dasatoo
Log in
Home
Courses
Tutors
Books
Q&A
Ask and answer questions to make information more available to wider audiences.
Tags
Latest
Ask Question
Avery Blain
@
blainavery
22, Aug 2023 12:00 AM
assign values to a matrix
How to assign values to a MATLAB matrix on the diagonal with vectorization?
answers
1
Answer 1
Chance Boorman
@
boormanchance
31, Aug 2023 10:48 AM
We can assign values to the diagonal of a matrix with the eye function.
rng(42)
A = magic(4)
values = [1,2,3,4]
A(logical(eye(size(A)))) = values
Output:
A =
1 2 3 13
5 2 10 8
9 7 3 12
4 14 15 4