@@ -1030,28 +1030,11 @@ function repmat(a::AbstractVector, m::Int)
1030
1030
return b
1031
1031
end
1032
1032
1033
- sub2ind (dims) = 1
1034
- sub2ind (dims, i:: Integer ) = int (i)
1035
- sub2ind (dims, i:: Integer , j:: Integer ) = sub2ind (dims, int (i), int (j))
1036
- sub2ind (dims, i:: Int , j:: Int ) = (j- 1 )* dims[1 ] + i
1037
- sub2ind (dims, i0:: Integer , i1:: Integer , i2:: Integer ) = sub2ind (dims, int (i0),int (i1),int (i2))
1038
- sub2ind (dims, i0:: Int , i1:: Int , i2:: Int ) =
1039
- i0 + dims[1 ]* ((i1- 1 ) + dims[2 ]* (i2- 1 ))
1040
- sub2ind (dims, i0:: Integer , i1:: Integer , i2:: Integer , i3:: Integer ) =
1041
- sub2ind (dims, int (i0),int (i1),int (i2),int (i3))
1042
- sub2ind (dims, i0:: Int , i1:: Int , i2:: Int , i3:: Int ) =
1043
- i0 + dims[1 ]* ((i1- 1 ) + dims[2 ]* ((i2- 1 ) + dims[3 ]* (i3- 1 )))
1044
-
1045
- function sub2ind (dims, I:: Integer... )
1046
- ndims = length (dims)
1047
- index = int (I[1 ])
1048
- stride = 1
1049
- for k= 2 : ndims
1050
- stride = stride * dims[k- 1 ]
1051
- index += (int (I[k])- 1 ) * stride
1052
- end
1053
- return index
1054
- end
1033
+ sub2ind (dims:: () ) = 1
1034
+ sub2ind (dims:: (Integer,) , i1:: Integer ) = i1
1035
+ sub2ind (dims:: (Integer,Integer) , i1:: Integer , i2:: Integer ) = i1+ dims[1 ]* (i2- 1 )
1036
+ sub2ind (dims:: (Integer,Integer,Integer...) , i1:: Integer , i2:: Integer , I:: Integer... ) =
1037
+ i1 + dims[1 ]* (sub2ind (tail (dims),i2,I... )- 1 )
1055
1038
1056
1039
function sub2ind {T<:Integer} (dims:: Array{T} , sub:: Array{T} )
1057
1040
ndims = length (dims)
@@ -1064,37 +1047,18 @@ function sub2ind{T<:Integer}(dims::Array{T}, sub::Array{T})
1064
1047
return ind
1065
1048
end
1066
1049
1067
- sub2ind {T<:Integer} (dims, I:: AbstractVector{T} ...) =
1050
+ sub2ind {T<:Integer} (dims:: (Integer...,) , I:: AbstractVector{T} ...) =
1068
1051
[ sub2ind (dims, map (X-> X[i], I)... ):: Int for i= 1 : length (I[1 ]) ]
1069
1052
1070
- function ind2sub (dims:: (Integer,Integer...) , ind:: Int )
1071
- ndims = length (dims)
1072
- stride = dims[1 ]
1073
- for i= 2 : ndims- 1
1074
- stride *= dims[i]
1075
- end
1076
-
1077
- sub = ()
1078
- for i= (ndims- 1 ): - 1 : 1
1079
- rest = rem (ind- 1 , stride) + 1
1080
- sub = tuple (div (ind - rest, stride) + 1 , sub... )
1081
- ind = rest
1082
- stride = div (stride, dims[i])
1083
- end
1084
- return tuple (ind, sub... )
1085
- end
1086
1053
1087
- ind2sub (dims:: (Integer...) , ind:: Integer ) = ind2sub (dims, int (ind))
1088
- ind2sub (dims:: () , ind:: Integer ) = ind== 1 ? () : throw (BoundsError ())
1089
- ind2sub (dims:: (Integer,) , ind:: Int ) = (ind,)
1090
- ind2sub (dims:: (Integer,Integer) , ind:: Int ) =
1091
- (rem (ind- 1 ,dims[1 ])+ 1 , div (ind- 1 ,dims[1 ])+ 1 )
1092
- ind2sub (dims:: (Integer,Integer,Integer) , ind:: Int ) =
1093
- (rem (ind- 1 ,dims[1 ])+ 1 , div (rem (ind- 1 ,dims[1 ]* dims[2 ]), dims[1 ])+ 1 ,
1094
- div (rem (ind- 1 ,dims[1 ]* dims[2 ]* dims[3 ]), dims[1 ]* dims[2 ])+ 1 )
1095
- ind2sub (a:: AbstractArray , ind:: Integer ) = ind2sub (size (a), Int (ind))
1054
+ ind2sub (dims:: () , ind:: Integer ) = ()
1055
+ ind2sub (dims:: (Integer,) , ind:: Integer ) = (ind,)
1056
+ ind2sub (dims:: (Integer,Integer) , ind:: Integer ) = (rem (ind- 1 ,dims[1 ])+ 1 ,div (ind- 1 ,dims[1 ])+ 1 )
1057
+ ind2sub (dims:: (Integer,Integer,Integer...) , ind:: Integer ) =
1058
+ tuple (rem (ind- 1 ,dims[1 ])+ 1 , ind2sub (tail (dims),div (ind- 1 ,dims[1 ])+ 1 )... )
1059
+ ind2sub (a:: AbstractArray , ind:: Integer ) = ind2sub (size (a), ind)
1096
1060
1097
- function ind2sub {T<:Integer} (dims:: (Integer,Integer ...) , ind:: AbstractVector{T} )
1061
+ function ind2sub {T<:Integer} (dims:: (Integer...) , ind:: AbstractVector{T} )
1098
1062
n = length (dims)
1099
1063
l = length (ind)
1100
1064
t = ntuple (n, x-> Array (Int, l))
@@ -1499,4 +1463,3 @@ function randsubseq!(S::AbstractArray, A::AbstractArray, p::Real)
1499
1463
end
1500
1464
1501
1465
randsubseq {T} (A:: AbstractArray{T} , p:: Real ) = randsubseq! (T[], A, p)
1502
-
0 commit comments