1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA - Serge STEER
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution. The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 function I=sub2ind(dims,varargin)
11 //sub2ind is used to determine the equivalent single index
12 //corresponding to a given set of subscript values.
14 //I = sub2ind(dims,i1,i2,..) returns the linear index equivalent to the
15 //row, column, ... subscripts in the arrays i1,i2,.. for an matrix of
18 //I = sub2ind(dims,Mi) returns the linear index
19 //equivalent to the n subscripts in the columns of the matrix Mi for a matrix
22 d=[1;cumprod(matrix(dims(1:$-1),-1,1))]
23 for i=1:size(varargin)
24 if varargin(i)==[] then I=[],return,end
27 if size(varargin)==1 then //subindices are the columns of the argument
29 else //subindices are given as separated arguments
31 for i=1:size(varargin)
32 I=I+(varargin(i)-1)*d(i)