subarray

version 1.1.0.0 (1.46 KB) by Peter Mao
Extracts a subarray from an array. Useful for function outputs.

831 Downloads

Updated 1 Aug 2011

View License

extracting a subarray from an array is simple:

a = 1:10;
a(4:8) %returns elements 4-8.

but sometimes, I have a function that returns an array, but I only want a subset of that array:

a = foo(x,y,z);
b = a(2,9:end);

Here 'a' is a temporary variable, polluting my workspace. Hence subarray:

b = subarray(foo(x,y,z),2,'9:end');

SUBARRAY should handle all the normal indexing cases (output of FIND, ranges, unspecified dimensions) just like the first example. The only exception is the 'end' keyword, where we have to pass in a string and EVAL it.

I tip my hat to Garret Euler of SUBMAT and SUBMAT_NOEVAL.

Cite As

Peter Mao (2022). subarray (https://www.mathworks.com/matlabcentral/fileexchange/24457-subarray), MATLAB Central File Exchange. Retrieved .

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

Community Treasure Hunt

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

Start Hunting!