File Exchange

image thumbnail

sub2allind

version 1.5.0.0 (2.69 KB) by Michael Völker
convert subscripts to ALL corresponding indices

646 Downloads

Updated 07 Feb 2012

View License

idx = sub2allind(sz, sub1, sub2, sub3, ... )

Like Matlab's sub2ind, sub2allind computes the equivalent linear indices for
given subscripts of an array with size SZ.
Unlike sub2ind, it computes a field of all combinations of
subscripts. So, instead of calling A( 2:3, 1, 4:11) you might
use
linIdx = sub2allind( size(A), 2:3, 1, 4:11 );

and then call A(linIdx) or A(linIdx(:)) or you reshape linIdx the way you need.

This is usefull whenever you want to access a subset of an array and compare it with a differently shaped thing, like

A(linIdx(:)) = A(linIdx(:)) + <1D-Vector>

Using the naked colon operator is allowed to reference an entire dimension:

linIdx = sub2allind( sz, :, sub2 );

Cite As

Michael Völker (2021). sub2allind (https://www.mathworks.com/matlabcentral/fileexchange/30096-sub2allind), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: CATPAD, N-dimensional sparse arrays

Community Treasure Hunt

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

Start Hunting!