MatLab Write a function called identity that creates a square identity matrix, w
ID: 3758323 • Letter: M
Question
MatLab
Write a function called identity that creates a square identity matrix, which is a matrix whose elements are 0 except for the elements on the diagonal (from top left to bottom right) which have a value of 1. The diagonal consists of those elements whose row and column indexes are the same: (1,1), (2,2), etc. The function takes one positive integer input argument, which is the size of the matrix, and returns the matrix itself as an output argument. For example, identity(4) must return a 4-by-4 identity matrix. You are not allowed to use the built-in eye or diag functions. (Hint: you can index into a matrix with a single index and MATLAB will handle it as if it was a vector using column-major order.)
Explanation / Answer
Matlab program to give out an identity matrix without using any built in functions:
clc;
clear all;
close all;
using zeros function:
clc;
clear all;
close all;