Using Loops to create a NxN identity Matrix

6 Ansichten (letzte 30 Tage)
Dave Smith
Dave Smith am 16 Feb. 2013
Write a function which creates and returns an N×N identity matrix. The function should accept the matrix size, N, as the only input. The function should set all elements of the matrix – not just the non-zero ones. The function should perform all operations on individual array elements (i.e. do not employ Matlab commands which operate on or create entire matrices/rows/columns).
  1 Kommentar
Dave Smith
Dave Smith am 16 Feb. 2013
The only thing I can think of doing is something like this
for N=[N]
eye(N)
end
It gets the correct answer, but I am not able to do this as stated it the problem.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 16 Feb. 2013
Using eye is explicitly not the way they want you to do it. Hint: I think they want you do do two for loops (nested), say one over r (for rows), and one over c (for columns) and an if inside where it r == c you set the array to 1 and otherwise set it equal to zero. Give that a try. Come back here with your code if it's not working out for you.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by