Be the first to rate this file! 2 Downloads (last 30 days) File Size: 1.72 KB File ID: #19498

Automatically arranged multiple subscripts from linear index

by Paul A.M. Bune

 

08 Apr 2008 (Updated 09 Apr 2008)

Functionally, the same as internal MatLab routine "ind2sub", but the result is put into an automatic

| Watch this File

File Information
Description

sub = ind2sub1(siz, ind)
 
Multiple subscripts (written into one vector or matrix) from linear index "ind" for an N-dimensional array defined by its array size "siz".
 
Linear index "ind" can be a single positive integer or a vector of positive integers. If "ind" is a row vector, it is converted into a column vector internally.
 
In principle, the same as MatLab routine "ind2sub", but the result is put into an automatically sized vector (if "ind" is a single integer) or matrix (if "ind" is a vector) rather than into multiple variables.
 
Example:
 
      A = ones(2, 3, 4);
      Sub1 = ind2sub1(size(A), 6)
 
      Sub1 =
 
           2 3 1
 
      Sub2 = ind2sub1(size(A), 4:4:24)
 
      Sub2 =
           2 2 1
           2 1 2
           2 3 2
           2 2 3
           2 1 4
           2 3 4

Note: An example of its application may be found in File ID 19484.

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
09 Apr 2008 Jos x@y.z

This can be quite simply accomplished by:

A = ones(2,3,4) ;
ind = 4:4:24 ;

[B{1:ndims(A)}] = ind2sub(size(A), ind(:)) ;
Sub2 = cat(2,B{:})

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
multiple subscripts Paul A.M. Bune 22 Oct 2008 09:56:47
index Paul A.M. Bune 22 Oct 2008 09:56:47
ind2sub Paul A.M. Bune 22 Oct 2008 09:56:47

Contact us at files@mathworks.com