# 2. Print the julia version
VERSION
v"0.3.0-prerelease+3551"
# 3. Create a null vector of size 10
Z = zeros(10)
10-element Array{Float64,1}: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
# 4. Create a null vector of size 10 but the fifth value which is 1
Z = zeros(10)
Z[5] = 1
Z
10-element Array{Float64,1}: 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
# 5. Create a vector with values ranging from 10 to 99
Z = [10:99]
90-element Array{Int64,1}: 10 11 12 13 14 15 16 17 18 19 20 21 22 ⋮ 88 89 90 91 92 93 94 95 96 97 98 99
# 6. Create a 3x3 matrix with values ranging from 0 to 8
Z = reshape(0:8, 3, 3)
3x3 Array{Int64,2}: 0 3 6 1 4 7 2 5 8
# 7. Find indices of non-zero elements from [1,2,0,0,4,0]
nz = find([1,2,0,0,4,0])
3-element Array{Int64,1}: 1 2 5
# 8. Create a 3x3 identity matrix
Z = eye(3)
3x3 Array{Float64,2}: 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0
# 9. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal
Z = diagm(1:4, -1)
5x5 Array{Int64,2}: 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 0
# 10. Create a 10x10x10 array with random values
rand(10, 10, 10)
10x10x10 Array{Float64,3}: [:, :, 1] = 0.233851 0.728841 0.869631 0.527396 … 0.363629 0.79046 0.891176 0.126595 0.929916 0.272223 0.256475 0.684575 0.84768 0.316753 0.633107 0.464661 0.293723 0.585927 0.472026 0.295694 0.891037 0.906112 0.22031 0.324554 0.8384 0.101888 0.0794546 0.448349 0.42875 0.694442 0.101025 0.375841 0.315316 0.723581 0.797002 0.0897314 0.826554 0.983008 0.898709 … 0.660895 0.490233 0.578319 0.635172 0.906699 0.703784 0.436587 0.0546955 0.166815 0.0605611 0.618516 0.879743 0.399733 0.362875 0.912985 0.941147 0.644874 0.484102 0.920259 0.241411 0.360327 0.61888 0.0406204 0.61829 0.31597 0.277052 0.441074 0.82985 0.989728 0.69032 0.057556 [:, :, 2] = 0.0980788 0.113146 0.83043 … 0.8716 0.288485 0.73808 0.0993029 0.606511 0.0102699 0.860618 0.793046 0.760717 0.677546 0.574312 0.0871516 0.279779 0.381178 0.850208 0.0299388 0.688978 0.728234 0.190331 0.268588 0.364282 0.320659 0.358252 0.267772 0.60241 0.496541 0.0203886 0.297899 0.229886 0.272618 … 0.279998 0.697106 0.463718 0.821681 0.81383 0.15778 0.149381 0.112835 0.930014 0.516927 0.0533908 0.076078 0.580471 0.259027 0.767634 0.987131 0.834645 0.564343 0.456745 0.811261 0.0989731 0.380874 0.844126 0.925517 0.608745 0.0985745 0.207258 [:, :, 3] = 0.643039 0.244599 0.73299 0.655386 … 0.621361 0.723289 0.692956 0.27256 0.189252 0.636373 0.879701 0.504404 0.11154 0.755994 0.574233 0.259239 0.570255 0.372934 0.217783 0.492578 0.604691 0.0115075 0.407045 0.834719 0.769053 0.439313 0.0884679 0.958531 0.955193 0.75849 0.935689 0.349299 0.690813 0.881236 0.703499 0.589765 0.339642 0.873172 0.509298 … 0.865166 0.514212 0.419344 0.783057 0.98835 0.223655 0.470435 0.867193 0.82926 0.390122 0.104309 0.0197628 0.248039 0.998866 0.998183 0.616856 0.896238 0.0736016 0.98574 0.399117 0.231014 0.310478 0.0495385 0.698049 0.979952 0.533324 0.11314 0.363453 0.87927 0.356386 0.441307 [:, :, 4] = 0.135566 0.116715 0.491633 0.0149035 … 0.145265 0.755245 0.395141 0.299962 0.59717 0.379739 0.0467359 0.226347 0.540538 0.0692494 0.766399 0.595497 0.419829 0.809562 0.748844 0.0807632 0.902657 0.67985 0.946815 0.234981 0.116237 0.604164 0.53564 0.0405198 0.569681 0.726533 0.802501 0.807815 0.322187 0.028094 0.720428 0.369199 0.571661 0.740962 0.0923443 … 0.737665 0.10973 0.319053 0.939227 0.375794 0.600945 0.806217 0.784005 0.43396 0.665886 0.91851 0.240313 0.823973 0.709321 0.892439 0.743397 0.174717 0.956367 0.892758 0.650538 0.820477 0.443747 0.501299 0.793012 0.681086 0.615381 0.22929 0.879795 0.689571 0.544099 0.109043 [:, :, 5] = 0.509388 0.990694 0.361262 … 0.791427 0.305971 0.263641 0.00474269 0.677085 0.00213538 0.589583 0.0316549 0.0258197 0.901766 0.0833243 0.876426 0.94626 0.72914 0.0562353 0.151311 0.51002 0.024961 0.969243 0.242962 0.293492 0.417311 0.196949 0.476736 0.00917061 0.245741 0.985759 0.961225 0.881274 0.0940466 … 0.809403 0.00347048 0.403088 0.855868 0.125295 0.640858 0.396523 0.689493 0.558125 0.319945 0.0692547 0.141662 0.395028 0.245471 0.150383 0.0753376 0.854093 0.873871 0.487085 0.24953 0.130792 0.33125 0.935829 0.645617 0.95305 0.722772 0.592601 [:, :, 6] = 0.58191 0.621594 0.916021 … 0.389773 0.27041 0.488904 0.623958 0.368572 0.21812 0.062457 0.226509 0.310958 0.230424 0.180556 0.271554 0.0970325 0.557498 0.476869 0.233895 0.925058 0.00442943 0.455689 0.302779 0.311177 0.0495436 0.0396511 0.857677 0.63925 0.0294536 0.92086 0.0152908 0.437925 0.574839 … 0.232824 0.355263 0.102309 0.720121 0.880921 0.971627 0.160208 0.587154 0.187366 0.797932 0.618252 0.0312813 0.30995 0.0358467 0.569837 0.840336 0.944605 0.846964 0.292689 0.805721 0.844546 0.417867 0.335175 0.972247 0.184364 0.234825 0.372994 [:, :, 7] = 0.236981 0.389883 0.74657 … 0.581281 0.249207 0.503852 0.530518 0.165866 0.0962068 0.48889 0.343129 0.260492 0.495289 0.23642 0.230225 0.188955 0.495804 0.318558 0.519342 0.894187 0.255443 0.91247 0.0289258 0.736533 0.974299 0.186031 0.69465 0.114214 0.672251 0.349018 0.691273 0.764584 0.538521 … 0.998093 0.402795 0.919574 0.456819 0.0193473 0.688954 0.000807909 0.212107 0.402013 0.0631101 0.252299 0.226128 0.500369 0.574163 0.59788 0.398997 0.811111 0.589637 0.622554 0.0791389 0.510188 0.915474 0.354691 0.305786 0.468984 0.359962 0.674837 [:, :, 8] = 0.410018 0.607212 0.927567 0.691498 … 0.575314 0.532166 0.102561 0.651431 0.316656 0.569273 0.826319 0.0793664 0.491583 0.268313 0.690124 0.454243 0.576144 0.757246 0.795331 0.773236 0.490231 0.75006 0.511879 0.262802 0.498337 0.87026 0.418783 0.323627 0.108954 0.990431 0.249158 0.0680794 0.213924 0.770508 0.49906 0.274915 0.72513 0.436055 0.0330413 … 0.995791 0.728605 0.00985385 0.532112 0.979463 0.942414 0.342541 0.453797 0.28847 0.191818 0.103067 0.819755 0.690357 0.547487 0.136401 0.344162 0.695057 0.900266 0.169922 0.968981 0.744517 0.0340046 0.75649 0.640033 0.911729 0.566299 0.842337 0.923129 0.859536 0.69058 0.994573 [:, :, 9] = 0.91899 0.0755625 0.856811 … 0.61104 0.621683 0.528942 0.114164 0.961095 0.0948555 0.627108 0.394597 0.0633887 0.993322 0.75322 0.657653 0.482234 0.725952 0.742763 0.603982 0.877212 0.309417 0.950757 0.284081 0.595431 0.00846731 0.21678 0.461366 0.822574 0.305982 0.457417 0.501352 0.264464 0.421689 … 0.153727 0.681476 0.746244 0.137745 0.233856 0.128829 0.00608756 0.146637 0.986387 0.803436 0.32966 0.486294 0.618969 0.818622 0.846825 0.0917961 0.872192 0.416664 0.0845489 0.296914 0.419933 0.604282 0.660882 0.593947 0.821882 0.503352 0.656685 [:, :, 10] = 0.297338 0.197652 0.930051 0.865109 … 0.204957 0.28761 0.140035 0.903968 0.259236 0.878565 0.900742 0.20523 0.624095 0.411187 0.826934 0.512341 0.322212 0.249611 0.0505103 0.927662 0.14997 0.919597 0.56048 0.78677 0.632403 0.024862 0.615981 0.157136 0.434046 0.849311 0.351699 0.661069 0.325086 0.455017 0.178995 0.138912 0.339827 0.240971 0.330819 … 0.271262 0.606077 0.547033 0.942544 0.359972 0.190648 0.209371 0.0766994 0.7182 0.559834 0.0639515 0.0952554 0.877573 0.749736 0.164641 0.469483 0.158272 0.304955 0.423725 0.968993 0.903195 0.0454189 0.671364 0.418271 0.4136 0.770848 0.586366 0.947543 0.0115889 0.0958372 0.938493
# Novice
# 1. Create a 8x8 matrix and fill it with a checkerboard pattern
Z = zeros(Int64,8,8)
Z[1:2:end, 2:2:end] = 1
Z[2:2:end, 1:2:end] = 1
Z
# Another solution
# Author: harven
[(i+j)%2 for i=1:8, j=1:8]
8x8 Array{Int64,2}: 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
# 2. Create a 10x10 array with random values and find the minimum and maximum values
Z = rand(10, 10)
Zmin, Zmax = minimum(Z), maximum(Z)
# It also write as following. thanks [hc_e](http://qiita.com/chezou/items/d7ca4e95d25835a5cd01#comment-1c20073a44695c08f523)
Zmin, Zmax = extrema(Z)
(0.0025525641918013875,0.999393511526921)
# 3. Create a checkerboard 8x8 matrix using the tile function
# numpy's tile equal to repmat
Z = repmat([0 1;1 0],4,4)
8x8 Array{Int64,2}: 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
# 4. Normalize a 5x5 random matrix (between 0 and 1)
Z = rand(5, 5)
Zmin, Zmax = minimum(Z), maximum(Z)
Z = (Z .- Zmin)./(Zmax - Zmin)
5x5 Array{Float64,2}: 0.167605 0.906165 0.209344 0.142284 0.533567 0.472184 0.347995 1.0 0.0 0.41441 0.108546 0.0599512 0.473854 0.523577 0.0626285 0.0326418 0.164949 0.85084 0.552733 0.791898 0.984592 0.732784 0.453393 0.512974 0.424608
# 5. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
Z = ones(5,3) * ones(3,2)
5x2 Array{Float64,2}: 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0
# 6. Create a 10x10 matrix with row values ranging from 0 to 9
(zeros(Int64,10,10) .+ [0:9])'
# Alternate solution
# Author: Leah Hanson
[y for x in 1:10, y in 0:9]
10x10 Array{Int64,2}: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
# 7. Create a vector of size 1000 with values ranging from 0 to 1, both excluded
linspace(0,1, 1002)[2:end - 1]
1000-element Array{Float64,1}: 0.000999001 0.001998 0.002997 0.003996 0.004995 0.00599401 0.00699301 0.00799201 0.00899101 0.00999001 0.010989 0.011988 0.012987 ⋮ 0.988012 0.989011 0.99001 0.991009 0.992008 0.993007 0.994006 0.995005 0.996004 0.997003 0.998002 0.999001
# 8. Create a random vector of size 100 and sort it
Z = rand(100)
sort(Z) # returns a sorted copy of Z; leaves Z unchanged
# Alternate solution
# Author: Leah Hanson
Z = rand(100)
sort!(Z) # sorts Z in-place; returns Z
100-element Array{Float64,1}: 0.0103796 0.0259711 0.0475339 0.0551598 0.0587751 0.0601322 0.0928093 0.0943298 0.0962813 0.119288 0.11995 0.12609 0.134203 ⋮ 0.918914 0.921578 0.930456 0.936039 0.936989 0.944145 0.949722 0.952165 0.965933 0.965948 0.967604 0.99916
# 9. Consider two random matrices A anb B, check if they are equal.
A = rand(0:2, 2,2)
B = rand(0:2, 2,2)
A == B
false
# 10. Create a random vector of size 1000 and find the mean value
Z = rand(1000)
m = mean(Z)
0.5042281294496798
# Apprentice
# 1. Make an array immutable (read-only)
# nothing
# 2. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates
Z = rand(10,2)
X, Y = Z[:,1], Z[:,2]
R = sqrt(X.^2 + Y.^2)
T = atan2(Y,X)
10-element Array{Float64,1}: 1.21097 0.243687 0.822103 0.705049 1.43544 0.329852 1.01906 1.07077 1.3239 0.315829
# 3. Create random vector of size 100 and replace the maximum value by 0
Z = rand(100)
Z[indmax(Z)] = 0
0
# 4. Create a structured array with x and y coordinates covering the [0,1]x[0,1] area.
# There is not exist `meshgrid` officially
# see also. https://github.com/JuliaLang/julia/issues/4093
# assume using https://github.com/JuliaLang/julia/blob/master/examples/ndgrid.jl
include("/Applications/Julia-0.3.0-prerelease-547facf2c1.app/Contents/Resources/julia/share/julia/examples/ndgrid.jl")
X = linspace(0,1,10)
Zx, Zy = meshgrid(X, X)
# Another solution
# Author: Alireza Nejati
[(x,y) for x in linspace(0,1,10), y in linspace(0,1,10)]
10x10 Array{(Float64,Float64),2}: (0.0,0.0) … (0.0,1.0) (0.1111111111111111,0.0) (0.1111111111111111,1.0) (0.2222222222222222,0.0) (0.2222222222222222,1.0) (0.3333333333333333,0.0) (0.3333333333333333,1.0) (0.4444444444444444,0.0) (0.4444444444444444,1.0) (0.5555555555555556,0.0) … (0.5555555555555556,1.0) (0.6666666666666666,0.0) (0.6666666666666666,1.0) (0.7777777777777778,0.0) (0.7777777777777778,1.0) (0.8888888888888888,0.0) (0.8888888888888888,1.0) (1.0,0.0) (1.0,1.0)
# 5. Print the minimum and maximum representable value for each Julia scalar type
for dtype in (Int8, Int16, Int32, Int64)
println(typemin(dtype))
println(typemax(dtype))
end
# Another solution
# Author: harven
print(map!(t -> (typemin(t),typemax(t)), subtypes(Signed)))
# typemin, typemax returns -Inf, Inf
for dtype in (Float32, Float64)
println(typemin(dtype))
println(typemax(dtype))
println(eps(dtype))
end
-128 127 -32768 32767 -2147483648 2147483647 -9223372036854775808 9223372036854775807 {(-170141183460469231731687303715884105728,170141183460469231731687303715884105727),(-32768,32767),(-2147483648,2147483647),(-9223372036854775808,9223372036854775807),(-128,127)}-Inf Inf 1.1920929e-7 -Inf Inf 2.220446049250313e-16
# 6. Create a structured array representing a position (x,y) and a color (r,g,b)
# Julia doesn't have StructArray
# see also: https://github.com/JuliaLang/julia/issues/1263
# use DataFrames
# 7. Consider a random vector with shape (100,2) representing coordinates, find point by point distances
Z = rand(10,2)
X,Y = Z[:,1], Z[:,2]
D = sqrt((X.-X').^2 + (Y .- Y').^2)
10x10 Array{Float64,2}: 0.0 0.776522 0.755307 0.129905 … 0.558582 0.857336 0.80191 0.776522 0.0 0.0649081 0.861585 0.395302 0.79179 0.51461 0.755307 0.0649081 0.0 0.832839 0.337819 0.838876 0.567363 0.129905 0.861585 0.832839 0.0 0.591631 0.986796 0.928192 0.79448 0.389001 0.444782 0.914696 0.684248 0.419097 0.130588 1.12902 0.45689 0.518572 1.23795 … 0.851017 0.723674 0.466754 0.74791 0.551813 0.599229 0.876053 0.777664 0.239993 0.0801106 0.558582 0.395302 0.337819 0.591631 0.0 0.996113 0.777951 0.857336 0.79179 0.838876 0.986796 0.996113 0.0 0.289162 0.80191 0.51461 0.567363 0.928192 0.777951 0.289162 0.0
# 8. Generate a generic 2D Gaussian-like array
X, Y = meshgrid(linspace(-1,1,100),linspace(-1,1,100))
D = sqrtm(X*X + Y*Y)
sigma, mu = 1.0, 0.0
G = exp(-( (D.-mu)^2 / ( 2.0 * sigma^2 ) ) )
# Another solution
# Author: Billou Beilour
sigma, mu = 1.0, 0.0
G = [ exp(-(x-mu).^2/(2.0*sigma^2) -(y-mu).^2/(2.0*sigma^2) ) for x in linspace(-1,1,100), y in linspace(-1,1,100) ]
# One more solution
# Author: Billou Beilour
sigma, mu = 1.0, 0.0
x,y = linspace(-1,1,100), linspace(-1,1,100)
G = zeros(length(x),length(y))
for i in 1:length(x), j in 1:length(y)
G[i,j] = exp(-(x[i]-mu).^2/(2.0*sigma^2) -(y[j]-mu).^2/(2.0*sigma^2) )
end
# 9. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value ?
Z = [1,2,3,4,5]
nz = 3
Z0 = zeros(length(Z) + (length(Z)-1)*(nz))
Z0[1:nz+1:end] = Z
5-element Array{Int64,1}: 1 2 3 4 5
# 10. Find the nearest value from a given value in an array
Z = [3,6,9,12,15]
Z[indmin(abs(Z .- 10))]
9
# Journyman
# 1. Consider the following file:
# 1,2,3,4,5
# 6,,,7,8
# ,,9,10,11
# How to read it ?
using DataFrames
readtable("missing.dat")
varzm! not defined while loading /Users/mic/.julia/v0.3/StatsBase/src/moments.jl, in expression starting on line 26 while loading /Users/mic/.julia/v0.3/StatsBase/src/StatsBase.jl, in expression starting on line 141 while loading /Users/mic/.julia/v0.3/DataFrames/src/DataFrames.jl, in expression starting on line 11 while loading In[30], in expression starting on line 7 in include at boot.jl:244 (repeats 2 times) in reload_path at loading.jl:152 in _require at loading.jl:67 in require at loading.jl:54 in include at boot.jl:244 in reload_path at loading.jl:152 in _require at loading.jl:67 in require at loading.jl:51
# 2. Consider a generator function that generates 10 integers and use it to build an array
# I can't translate this question
# 3. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices) ?
using StatsBase
Z = ones(10)
I = rand(0:length(Z), 20)
Z += counts(I, 1:length(Z))
counts not defined while loading In[32], in expression starting on line 5
# 4. How to accumulate elements of a vector (X) to an array (F) based on an index list (I) ?
using StatsBase
X = WeightVec([1,2,3,4,5,6])
I = [1,3,9,3,4,1]
F = counts(I, maximum(I), X)
counts not defined while loading In[33], in expression starting on line 5
# 5. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors
w,h = 16,16
I = convert(Array{Uint8}, rand(0:2, (h,w,3)))
F = I[:,:,1] * 256 * 256 + I[:,:,2]*256 + I[:,:,3]
n = length(unique(F))
unique(I)
3-element Array{Uint8,1}: 0x02 0x01 0x00
# 6. Considering a four dimensions array, how to get sum over the last two axis at once ?
A = rand(0:10, (3,4,3,4))
x,y = size(A)[1:end-2]
z = prod(size(A)[end-1:end])
calc_sum = sum(reshape(A, (x,y,z)),3)
3x4x1 Array{Int64,3}: [:, :, 1] = 61 64 42 80 65 47 63 64 63 71 44 34
# 7. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices ?
using StatsBase
D = WeightVec(rand(100))
S = rand(0:10,100)
D_sums = counts(S, maximum(S), D)
D_counts = counts(S, maximum(S))
D_means = D_sums ./ D_counts
counts not defined while loading In[36], in expression starting on line 5
# Craftsman
# 1. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1])
# I don't find any function like stride_tricks.as_stride
function rolling(A, window)
Z = zeros(length(A)-2, window)
for i in 1:(length(A) - window +1)
Z[i,:] = A[i:i+2]
end
return Z
end
rolling(0:100, 3)
99x3 Array{Float64,2}: 0.0 1.0 2.0 1.0 2.0 3.0 2.0 3.0 4.0 3.0 4.0 5.0 4.0 5.0 6.0 5.0 6.0 7.0 6.0 7.0 8.0 7.0 8.0 9.0 8.0 9.0 10.0 9.0 10.0 11.0 10.0 11.0 12.0 11.0 12.0 13.0 12.0 13.0 14.0 ⋮ 87.0 88.0 89.0 88.0 89.0 90.0 89.0 90.0 91.0 90.0 91.0 92.0 91.0 92.0 93.0 92.0 93.0 94.0 93.0 94.0 95.0 94.0 95.0 96.0 95.0 96.0 97.0 96.0 97.0 98.0 97.0 98.0 99.0 98.0 99.0 100.0
# 2. Consider a set of 100 triplets describing 100 triangles (with shared vertices), find the set of unique line segments composing all the triangles.
faces = rand(0:100, 100, 3)
face2 = kron(faces,[1 1])
F = circshift(sortcols(face2),(0,1))
F = reshape(F, (convert(Int64,length(F)/2),2))
F = sort(F,2)
G = unique(F,1)
287x2 Array{Int64,2}: 78 94 42 53 60 67 2 77 45 70 41 49 16 97 30 62 6 44 0 75 35 91 15 88 0 82 ⋮ 75 89 40 56 62 73 59 72 36 92 23 97 49 82 26 44 6 22 79 89 21 93 79 83
# 3. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C ?
using StatsBase
O = [1 1 2 3 4 4 6]
C = counts(O, maximum(O))
A = foldl(vcat,[kron(ones(Int64, C[i]), i) for i in 1:length(C)])
counts not defined while loading In[39], in expression starting on line 4
# 4. How to compute averages using a sliding window over an array ?
function moving_average(A, n=3)
ret = cumsum(A)
ret[n+1:end] = ret[n+1:end] - ret[1:end-n]
return ret[n:end-1] / n
end
Z = 0:20
moving_average(Z, 3)
18-element Array{Float64,1}: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0
# Artisan
# 1. Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3])
Z = rand(0:5,100,3)
E = prod(Z[:,2:end] .== Z[:,1:end-1],2)
U = Z[find(~E), :]
99x3 Array{Int64,2}: 2 5 1 3 2 0 3 3 2 3 1 1 1 1 0 1 2 3 5 3 1 2 1 1 5 5 3 3 1 2 0 0 1 2 5 1 5 1 3 ⋮ 2 5 0 3 1 3 4 2 5 0 2 4 3 0 3 1 5 5 2 4 5 0 2 4 3 0 1 5 3 3 5 1 2 3 4 0
# 2. Convert a vector of ints into a matrix binary representation.
I = [0 1 2 3 15 16 32 64 128]
B = foldl(hcat,[reverse(int(bool(i & (2 .^ (0:8))))) for i in I])'
9x9 Array{Int64,2}: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# Adept
# 1. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary)
# TBS
# Expert
# 1. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B ?
# I can't execute numpy version...
# 2. Extract all the contiguous 3x3 blocks from a random 10x10 matrix.
# TBS
# 3. Extract all the contiguous 3x3 blocks from a random 10x10 matrix.
# There is Symmetric class in julia but immutable
# https://github.com/JuliaLang/julia/blob/master/base/linalg/symmetric.jl
# See also: https://github.com/JuliaLang/julia/pull/1533
# 4. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once ? (result has shape (n,1))
p, n = 10, 20
M = ones(n,n,p)
V = ones(n,p)
S = reduce(+, [M[i,:,j]*V[i] for i = 1:n, j = 1:p])'
S
20x1 Array{Float64,2}: 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0 200.0
# Master
# 1. Given a two dimensional array, how to extract unique rows ?
Z = rand(0:2, 6,3)
uZ = unique(Z,1)
6x3 Array{Int64,2}: 2 0 1 2 0 2 0 1 0 2 1 1 0 2 0 1 0 0