(BUTTON) Toggle Main Navigation MathWorks * Products * Solutions * Academia * Support * Community * Events * Contact Us * Get MATLAB MathWorks * Products * Solutions * Academia * Support * Community * Events * Get MATLAB * Documentation (BUTTON) * All * Examples * Functions * More Search MATLAB Documentation ____________________ (BUTTON) Documentation * Support * MathWorks (BUTTON) Search All Support Resources ____________________ (BUTTON) Support * Documentation * MathWorks (BUTTON) Search MathWorks.com ____________________ (BUTTON) MathWorks * Documentation * Support (BUTTON) (BUTTON) Close Mobile Search (BUTTON) Open Mobile Search * All * Examples * Functions (BUTTON) Toggle navigation * Trial Software * Product Updates * Documentation Home * MATLAB * Language Fundamentals * Matrices and Arrays * MATLAB * Graphics * Graphics Objects * Object Containers * eye * On this page * Syntax * Description * Examples + Square Identity Matrix + Rectangular Matrix + Identity Vector + Nondefault Numeric Data Type + Complex Identity Matrix + Sparse Identity Matrix + Size and Numeric Data Type Defined by Existing Array * Input Arguments + n + m + sz + typename + p * Extended Capabilities * See Also eye Identity matrix collapse all in page Syntax I = eye I = eye(n) I = eye(n,m) I = eye(sz) I = eye(___,typename) I = eye(___,'like',p) Description I = eye returns the scalar 1. example I = eye(n) returns an n-by-n identity matrix with ones on the main diagonal and zeros elsewhere. example I = eye(n,m) returns an n-by-m matrix with ones on the main diagonal and zeros elsewhere. example I = eye(sz) returns an array with ones on the main diagonal and zeros elsewhere. The size vector, sz, defines size(I). For example, eye([2,3]) returns a 2-by-3 array with ones on the main diagonal and zeros elsewhere. example I = eye(___,typename) also specifies the data type (class) of I for any of the previous syntaxes. For example, eye(5,'int8') returns a 5-by-5 identity matrix consisting of 8-bit integers. example I = eye(___,'like',p) specifies that I has the same data type, sparsity, and complexity (real or complex) as the numeric variable p. Examples collapse all Square Identity Matrix Open Live Script Create a 4-by-4 identity matrix. I = eye(4) I = 4×4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Rectangular Matrix Open Live Script Create a 2-by-3 identity matrix. I = eye(2,3) I = 2×3 1 0 0 0 1 0 Identity Vector Open Live Script Create a 3-by-1 identity vector. sz = [3,1]; I = eye(sz) I = 3×1 1 0 0 Nondefault Numeric Data Type Open Live Script Create a 3-by-3 identity matrix whose elements are 32-bit unsigned integers. I = eye(3,'uint32'), I = 3x3 uint32 matrix 1 0 0 0 1 0 0 0 1 class(I) ans = 'uint32' Complex Identity Matrix Open Live Script Create a 2-by-2 identity matrix that is not real valued, but instead is complex like an existing array. Define a complex vector. p = [1+2i 3i]; Create an identity matrix that is complex like p. I = eye(2,'like',p) I = 2×2 complex 1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i Sparse Identity Matrix Open Live Script Define a 5-by-5 sparse matrix. p = sparse(5,5,pi); Create a 5-by-5 identity matrix that is sparse like P. I = eye(5,'like',p) I = (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1 Size and Numeric Data Type Defined by Existing Array Open Live Script Define a 2-by-2 matrix of single precision. p = single([1 3 ; 2 4]); Create an identity matrix that is the same size and data type as P. I = eye(size(p),'like',p), I = 2x2 single matrix 1 0 0 1 class(I) ans = 'single' Input Arguments collapse all n — Size of first dimension of I integer value Size of first dimension of I, specified as an integer value. * If n is the only integer input argument, then I is a square n-by-n identity matrix. * If n is 0, then I is an empty matrix. * If n is negative, then it is treated as 0. Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 m — Size of second dimension of I integer value Size of second dimension of I, specified as an integer value. * If m is 0, then I is an empty matrix. * If m is negative, then it is treated as 0. Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 sz — Size of I row vector of no more than two integer values Size of I, specified as a row vector of no more than two integer values. * If an element of sz is 0, then I is an empty matrix. * If an element of sz is negative, then the element is treated as 0. Example: sz = [2,3] defines I as a 2-by-3 matrix. Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 typename — Output class 'double' (default) | 'single' | 'logical' | 'int8' | 'uint8' | ... Output class, specified as 'double', 'single', logical, 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', or 'uint64'. p — Prototype numeric variable Prototype, specified as a numeric variable. Data Types: double | single | logical | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 Complex Number Support: Yes Extended Capabilities C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. Usage notes and limitations: * typename must be a built-in MATLAB^® numeric type. Does not invoke the static eye method for other classes. For example, eye(m, n, 'myclass') does not invoke myclass.eye(m,n). * Size arguments must have a fixed size. GPU Code Generation Generate CUDA® code for NVIDIA® GPUs using GPU Coder™. Usage notes and limitations: * typename must be a built-in MATLAB numeric type. Does not invoke the static eye method for other classes. For example, eye(m, n, 'myclass') does not invoke myclass.eye(m,n). * Size arguments must have a fixed size. GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Usage notes and limitations: * See eye in the Parallel Computing Toolbox™ documentation. Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. Usage notes and limitations: * See eye in the Parallel Computing Toolbox documentation. See Also ones | speye | zeros Topics * Class Support for Array-Creation Functions Introduced before R2006a (BUTTON) × Open Example A modified version of this example exists on your system. Do you want to open this version instead? No, overwrite the modified version Yes (BUTTON) × MATLAB Command You clicked a link that corresponds to this MATLAB command: Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands. (BUTTON) Close (BUTTON) × MathWorks Select a Web Site Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: . Select web site You can also select a web site from the following list: How to Get Best Site Performance Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location. Americas * América Latina (Español) * Canada (English) * United States (English) Europe * Belgium (English) * Denmark (English) * Deutschland (Deutsch) * España (Español) * Finland (English) * France (Français) * Ireland (English) * Italia (Italiano) * Luxembourg (English) * Netherlands (English) * Norway (English) * Österreich (Deutsch) * Portugal (English) * Sweden (English) * Switzerland + Deutsch + English + Français * United Kingdom (English) Asia Pacific * Australia (English) * India (English) * New Zealand (English) * 中国 + 简体中文 + English * 日本 (日本語) * 한국 (한국어) Contact your local office * Trial Software * Product Updates MATLAB Documentation * Functions * Release Notes * PDF Documentation Support * MATLAB Answers * Installation Help * Bug Reports * Product Requirements * Software Downloads Introducing Deep Learning with MATLAB Introducing Deep Learning with MATLAB Download ebook Explore Products * MATLAB * Simulink * Student Software * Hardware Support * File Exchange Try or Buy * Downloads * Trial Software * Contact Sales * Pricing and Licensing * How to Buy Learn to Use * Documentation * Tutorials * Examples * Videos and Webinars * Training Get Support * Installation Help * Answers * Consulting * License Center About MathWorks * Careers * Newsroom * Social Mission * Contact Us * About MathWorks MathWorks Accelerating the pace of engineering and science MathWorks is the leading developer of mathematical computing software for engineers and scientists. Discover... * United States * Patents * Trademarks * Privacy Policy * Preventing Piracy * Application Status © 1994-2020 The MathWorks, Inc. * Facebook * Twitter * Instagram * YouTube * LinkedIn * RSS Join the conversation