Function that returns an NxN matrix

2 views (last 30 days)
I need to create a function that when a value for N is entered it returns an NxN identity matrix. But this needs to be done without employing Matlab commands which operate on entire matrices, rows, and columns.
  2 Comments
Andrei Bobrov
Andrei Bobrov on 8 Oct 2014
in MATLAB without employing Matlab commands - how?
Stephen23
Stephen23 on 8 Oct 2014
All functions require MATLAB commands, starting with the function function.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 8 Oct 2014
for jj = N:-1:1
out(jj,jj) = 1;
end
  5 Comments
Stephen23
Stephen23 on 9 Oct 2014
Edited: Stephen23 on 9 Oct 2014
Andrei Bobrov: I like the array preallocation through the reversed sequence. That is a nice touch!
Andrei Bobrov
Andrei Bobrov on 10 Oct 2014
Hi Stephen! This is idea by Jan Simon.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!