Skip to content

Commit

Permalink
Limited repelemM
Browse files Browse the repository at this point in the history
Only for vector and a scalar pair (#4)
  • Loading branch information
aminya committed Sep 22, 2019
1 parent a940b7a commit 236834f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/usage_Matrices_and_Arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ mHCat1 = horzcatM(ones(3, 3), zeros(3, 3)) # [ones(3, 3) zeros(3, 3)]
################################################################
mVCat1 = vertcatM(ones(3, 3), zeros(3, 3)) # [ones(3, 3); zeros(3, 3)]
################################################################
V1 = [1 2 3 4];
mRepelem1 = repelemM(V1, 3) # [1 1 1 2 2 2 3 3 3 4 4 4]

# mRepelem2 = repelemM(V1, (3,2,1,1)) # [1 1 1 2 2 3 4]

# mRepelem3 = repelemM(V1, vec([3 2 1 1])) # [1 1 1 2 2 3 4]
################################################################
################################################################
A1 = ones(2, 1, 2); # 3 dimensional
mSqueeze1 = squeezeM(A1) # [1 1; 1 1]
Expand Down
19 changes: 19 additions & 0 deletions src/Language_Fundamentals/Matrices_and_Arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,25 @@ mVCat1 = vertcatM(ones(3, 3), zeros(3, 3)) # [ones(3, 3); zeros(3, 3)]
"""
const vertcatM = vcat
################################################################
"""
repelemM(V, count)
Construct an array by repeating elements of array V by a given number of times specified by counts. if If count is a scalar, then each element of v is repeated "count" times
# Examples
```julia
V1 = [1 2 3 4];
mRepelem1 = repelemM(V1, 3) # [1 1 1 2 2 2 3 3 3 4 4 4]
```
"""
repelemM(A::AbstractArray, count::Integer) = vcat(fill.(A, count)...);

# For full support wait until this is merged: https://github.com/JuliaLang/julia/pull/29560
# doc string to be added:
# , and if count is a vector of the same size of V, it will repeat each element of V corespondigly based on counts.
# repelemM(A, counts)
#
# Construct an array by repeating elements of array A by a given number of times in each dimension, specified by counts.
################################################################

"""
Expand Down
2 changes: 1 addition & 1 deletion src/MatLang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include("Language_Fundamentals/Entering_Commands.jl")

using LinearAlgebra

export zerosM, onesM, randM, eyeM, diagM, blkdiagM, catM, horzcatM, vertcatM, squeezeM
export zerosM, onesM, randM, eyeM, diagM, blkdiagM, catM, horzcatM, vertcatM, repelemM, squeezeM
include("Language_Fundamentals/Matrices_and_Arrays.jl")


Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ end
@test blkdiagM(ones(2, 4), 2 * ones(3, 2)) == [ones(2, 4) zeros(2, 2); zeros(3, 4) 2 * ones(3, 2)]
@test catM(1, ones(3, 3), zeros(3, 3), 2 * ones(3, 3)) == [ones(3, 3); zeros(3, 3); 2 * ones(3, 3)]
@test horzcatM(ones(3, 3), zeros(3, 3)) == [ones(3, 3) zeros(3, 3)]
@test repelemM([1 2 3 4], 3) == [1; 1; 1; 2; 2; 2; 3; 3; 3; 4; 4; 4]
@test squeezeM(ones(2,1,2)) == [1 1; 1 1]
end

0 comments on commit 236834f

Please sign in to comment.