-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsub2indq.m
64 lines (59 loc) · 2.11 KB
/
sub2indq.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
% Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
function ndx = sub2indq(siz,varargin)
%SUB2IND Linear index from multiple subscripts.
% DISABLED ERROR CHECKING FOR FASTER SPEED! JOCHER!!
% SUB2IND is used to determine the equivalent single index
% corresponding to a given set of subscript values.
%
% IND = SUB2IND(SIZ,I,J) returns the linear index equivalent to the
% row and column subscripts in the arrays I and J for a matrix of
% size SIZ.
%
% IND = SUB2IND(SIZ,I1,I2,...,IN) returns the linear index
% equivalent to the N subscripts in the arrays I1,I2,...,IN for an
% array of size SIZ.
%
% I1,I2,...,IN must have the same size, and IND will have the same size
% as I1,I2,...,IN. For an array A, if IND = SUB2IND(SIZE(A),I1,...,IN)),
% then A(IND(k))=A(I1(k),...,IN(k)) for all k.
%
% Class support for inputs I,J:
% float: double, single
%
% See also IND2SUB.
% Copyright 1984-2008 The MathWorks, Inc.
% $Revision: 1.14.4.8 $ $Date: 2008/05/23 15:34:46 $
%==============================================================================
siz = double(siz);
% if length(siz)<2
% error('MATLAB:sub2ind:InvalidSize',...
% 'Size vector must have at least 2 elements.');
% end
if length(siz) ~= nargin-1
%Adjust input
if length(siz)<nargin-1
%Adjust for trailing singleton dimensions
siz = [siz ones(1,nargin-length(siz)-1)];
else
%Adjust for linear indexing on last element
siz = [siz(1:nargin-2) prod(siz(nargin-1:end))];
end
end
%Compute linear indices
k = [1 cumprod(siz(1:end-1))];
ndx = 1;
%s = size(varargin{1}); %For size comparison
for i = 1:length(siz),
v = varargin{i};
%%Input checking
% if ~isequal(s,size(v))
% %Verify sizes of subscripts
% error('MATLAB:sub2ind:SubscriptVectorSize',...
% 'The subscripts vectors must all be of the same size.');
% end
% if (any(v(:) < 1)) || (any(v(:) > siz(i)))
% %Verify subscripts are within range
% error('MATLAB:sub2ind:IndexOutOfRange','Out of range subscript.');
% end
ndx = ndx + (v-1)*k(i);
end