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 Kommentare

Andrei Bobrov
Andrei Bobrov am 8 Okt. 2014
in MATLAB without employing Matlab commands - how?
Stephen23
Stephen23 am 8 Okt. 2014
All functions require MATLAB commands, starting with the function function.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 8 Okt. 2014

1 Stimme

for jj = N:-1:1
out(jj,jj) = 1;
end

5 Kommentare

Stephen23
Stephen23 am 8 Okt. 2014
This uses multiple MATLAB functions: for , colon , end ,...
Andrei Bobrov
Andrei Bobrov am 8 Okt. 2014
Hi Stephen! I agree with you.
Michael
Michael am 9 Okt. 2014
So I tried this in matlab and no matter what value you enter for N it displays a 4x4 identity matrix.
Stephen23
Stephen23 am 9 Okt. 2014
Bearbeitet: Stephen23 am 9 Okt. 2014
Andrei Bobrov: I like the array preallocation through the reversed sequence. That is a nice touch!
Andrei Bobrov
Andrei Bobrov am 10 Okt. 2014
Hi Stephen! This is idea by Jan Simon.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by