Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB Question

  1. Feb 13, 2009 #1
    Hi. I'm not sure if anyone here knows MATLAB, but I'm horrible with it. I have to write a program, which I actually figured out how to do, except that I have to first construct a certain matrix. I found one way to do it, but I'm sure it's not how my teacher wants it.

    Let A be the nxn matrix with elements aij = -1 for i > j, aii=ain=1, 1<=i<=n, and zero everywhere else.

    The only thing I've been successful with is to do this:

    B=eye(n,n);B(1:n,n)=1; (This puts 1's on the diagonal and in the last column)

    C=tril(-ones(n,n),-1); (This creates a lower triangular matrix with -1's below the diagonal)

    Then, A = B+C.

    I am sure there must be a more efficient way to construct this matrix that my teacher is expecting, but I'm not sure what it is. Does anyone who has experience with MATLAB know how I might be able to do this? Thank you!
  2. jcsd
  3. Feb 13, 2009 #2
    You could do it with for loops, but you've recognised some in-built functions of MATLAB and taken advantage of them (correctly!). The best person to ask what is required, of course, is your teacher!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook