Let’s do it in two simple lines and without zeros
… The first line creates a n x n
matrix where all elements are 0
. After you can (as your hint says) address the elements with a single argument. The distance between the ones in the identity matrix are n+1
. This way you write the ones with the mentioned distance until the end.
function out = identity(n)
out(n,n) = 0;
out(1:n+1:end) = 1;
end
Related Contents:
- How can I index a MATLAB array returned by a function without first assigning it to a local variable?
- Matrix of unknown length in MATLAB?
- How do you concatenate the rows of a matrix into a vector?
- How can I accumulate cells of different lengths into a matrix in MATLAB?
- How do I visualize a matrix with colors and values displayed?
- Is MATLAB row specific or column major?
- Use a vector as an index to a matrix
- Index of max and min value in an array
- How can I change the values of multiple points in a matrix?
- Construct adjacency matrix in MATLAB
- Replace specific columns in a matrix with a constant column vector
- How can I apply a function to every row/column of a matrix in MATLAB?
- How can I count the number of elements of a given value in a matrix?
- How to select a submatrix (not in any particular pattern) in Matlab
- Get the indices of the n largest elements in a matrix
- Multiply a 3D matrix with a 2D matrix
- How can I sort a 2-D array in MATLAB with respect to one column?
- How do I resize a matrix in MATLAB?
- Efficiently compute a 3D matrix of outer products – MATLAB
- Matrix “Zigzag” Reordering
- How can I divide each row of a matrix by a fixed row?
- Sorting entire matrix according to one column in matlab
- Matlab: repeat every column sequentially n times [duplicate]
- Saving time and memory using parfor?
- How to get all files under a specific directory in MATLAB?
- Generate random number with given probability matlab
- “Undefined function ‘function_name’ for input arguments of type ‘double’.”
- Efficient Implementation of `im2col` and `col2im`
- Getting FFT peaks from data
- Automatically plot different colored lines
- How to concatenate a number to a variable name in MATLAB?
- Understanding Matlab FFT example
- Suppress Output
- How to assign values to a MATLAB matrix on the diagonal?
- Stable accumarray in MATLAB
- The meaning of colon operator in MATLAB
- Return Unique Element with a Tolerance
- How to plot inequalities
- How does subplot work and what is the difference between subplot(121) and subplot(1,2,1) in MATLAB?
- pdist2 equivalent in MATLAB version 7
- MATLAB: Is it possible to overload operators on native constructs (cells, structs, etc)?
- Setting graph figure size
- Corner Cases, Unexpected and Unusual MATLAB [closed]
- What is a spectrogram and how do I set its parameters?
- How does the colon operator work in MATLAB?
- How to find all permutations (with repetition) in MATLAB?
- How to generate a non-linear colormap/colorbar?
- How to draw horizontal and vertical lines in MATLAB?
- how to replicate an array
- How to use SIFT algorithm to compute how similar two images are?