I am new to MATLAB and I want to write a simple program to generate an n x n matrix A such that:
1. a11 = 2
2. a22 = ... = ann = 5
3. 0 everywhere else
Can the program take n as an argument? So that I will have to choose an n every time I run the program.
Thanks!
n = 8; %# input value, matrix size
A = 5.*eye(n); %# put 5 on diagonal, zeros elsewhere
A(1,1) = 2; %# first element
Here's another way of doing the same.
n=8;
A=diag([2 5*ones(1,n-1)]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With