Doxygen Source Code Documentation
gind2sub.m
Go to the documentation of this file.00001 function [err, v] = gind2sub (sz, ndx)
00002 %
00003 % [err,] = gind2sub ()
00004 %
00005 %Purpose:
00006 % This function converts a linear index to multiple subscripts based on the dimensions.
00007 % It is a further modified version from Ziad's modification based on the original Matlab
00008 % function IND2SUB. The difference from what Ziad did is that we vary the subscript starting
00009 % from the last dimension and ends with the first dimension instead of the other way around.
00010 %
00011 %Input Parameters:
00012 % sz: dimension vector
00013 % ndx: counter index for the total number of combinations.
00014 %
00015 %Output Parameters:
00016 % err : 0 No Problem
00017 % : 1 Problems
00018 % v: array of subscripts
00019 %
00020 %Key Terms:
00021 %
00022 %More Info :
00023 %
00024 % Author : Gang Chen
00025 % Date : Tue Dec 23 10:57:48 EST 2003
00026 % SSCC/NIMH/ National Institutes of Health, Bethesda MD 20892
00027
00028
00029 %Define the function name for easy referencing
00030 FuncName = 'gind2sub';
00031
00032 %Debug Flag
00033 DBG = 1;
00034
00035 %initailize return variables
00036 err = 1;
00037
00038 v = [ones(1,length(sz))];
00039 if (length(ndx) ~= 1),
00040 fprintf(1,'Error zind2sub: length(IND) must be 1\n');
00041 v = [];
00042 return;
00043 end
00044
00045 n = length(sz);
00046 k = [1 cumprod(sz(1:end-1))];
00047 ndx = ndx - 1;
00048 for i = n:-1:1,
00049 %for i = 1:1:n,
00050 v(i) = floor(ndx/k(i))+1;
00051 ndx = rem(ndx,k(i));
00052 end
00053
00054 err = 0;
00055 return;
00056