File Exchange

image thumbnail

Torus Datatype

version (937 KB) by Steve McClure
Safely access out-of-bounds array indices using a Torus wrapper

1 Download

Updated 01 Sep 2016

View Version History

View License

A Torus is a geometric surface that can be represented by a doughnut: from any point on the surface, moving up/down or left/right will eventually lead you back to where you started, to continue on indefinitely. This has the capability of representing an infinite space with a finite amount of material.
The Torus datatype provides this functionality by wrapping the raw TrueData, and using (one-based) modular arithmetic to extend its space indefinitely. The subscript implementation is meant to be intuitive and match MATLAB's behavior of the builtin datatypes as closely as possible.
An easy example is the need to have the pattern [1 2 3] repeated end-over-end in a vector of length 20. A somewhat general solution might look like the following:

x = 1:3; % original data
n = 20;
y = repmat(x,1,ceil(n/length(x))); % make a new vector that is too big
y(1:n) % pick off the first n elements

Doing this with a Torus would be much more intuitive and efficient:

T = Torus(1:3);

Cite As

Steve McClure (2021). Torus Datatype (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Inspired: SpriteKit Framework

Community Treasure Hunt

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

Start Hunting!