Function that returns an NxN matrix

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 个评论

in MATLAB without employing Matlab commands - how?
All functions require MATLAB commands, starting with the function function.

请先登录,再进行评论。

 采纳的回答

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

5 个评论

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

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by