diff --git a/examples/usage_Matrices_and_Arrays.jl b/examples/usage_Matrices_and_Arrays.jl index 6a39b44..657804d 100644 --- a/examples/usage_Matrices_and_Arrays.jl +++ b/examples/usage_Matrices_and_Arrays.jl @@ -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] diff --git a/src/Language_Fundamentals/Matrices_and_Arrays.jl b/src/Language_Fundamentals/Matrices_and_Arrays.jl index ea08ed4..7c1b59b 100644 --- a/src/Language_Fundamentals/Matrices_and_Arrays.jl +++ b/src/Language_Fundamentals/Matrices_and_Arrays.jl @@ -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. ################################################################ """ diff --git a/src/MatLang.jl b/src/MatLang.jl index 69d5fbd..4ab109d 100644 --- a/src/MatLang.jl +++ b/src/MatLang.jl @@ -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") diff --git a/test/runtests.jl b/test/runtests.jl index f34fa79..49edf23 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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