Anti-diagonal matrix traversal

Version (1.43 KB) by Loïc
This function allows you to traverse a square matrix along the anti-diagonal.
Updated 21 May 2010

View License

This function takes as an input a square matrix and returns two outputs. The first is a vector containing the matrix values starting in the (1,1) entry and progressing along the anti-diagonal elements. A second output gives the sum of the elements along the anti-diagonals.
For example:
a = [1 2 3; 4 5 6; 7 8 9];
[b,c] = diagtrav(a);
b = [1 2 4 3 5 7 6 8 9];
c = [1 6 15 14 9];

Cite As

Loïc (2024). Anti-diagonal matrix traversal (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Operating on Diagonal Matrices in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes

- Standardized the function definition header
- Added input checking to verify input is a square matrix
- Pre-allocated arrays to reduce time

- Added input checking to verify the input is a square matrix
- Pre-allocated array sizes for speed improvment
- Modified the header