411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Exercises in Julia
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Exercises in Julia Predator-Prey Model Investing in options
1) Given a 5x5 matrix of random numbers, create a 5x1 vector containing the average of the matrix's columns:
using Statistics;
mA = rand(5, 5);
va=zeros(0);
for i in 1:5
va=append!(va,mean(mA[:,i]))
end
va
5-element Vector{Float64}:
0.43611016000428815
0.48149699521822703
0.3511122078447155
0.39808363881260017
0.42845881780212364
2) Given a 5x5 matrix of random numbers, create a 5x1 vector containing the sum of the matrix's rows:
mA = rand(5, 5);
va=zeros(0);
for i in 1:5
va=append!(va,sum(mA[i,:]))
end
va
5-element Vector{Float64}:
2.827351495353428
3.2196020670655634
2.5846498189690257
2.9442776092637275
2.936124612750376
3) In a 6x6 matrix, insert the value 1 in columns 2, 4 and rows 3, 6:
mA = zeros(6, 6);
mA[:, [2, 4]] .= 1;
mA[[3, 6], :] .= 1;
mA
6×6 Matrix{Float64}:
0.0 1.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 1.0 1.0
0.0 1.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 1.0 1.0
4) Given an 80x6 matrix of random numbers, construct a 16x6 matrix B in which each element contains the mean of 5 elements at a time, obtaining 16 means per column:
using Statistics;
v=range(0,step=5,stop=75);
A=rand(80,6)
n_cols=size(A,2)
B= zeros(16,n_cols)
for i in 1:n_cols
z=0
for j in v
z=z+1
B[z,i]=mean(A[j+1:j+5,i])
end
end
B
16×6 Matrix{Float64}:
0.493918 0.426822 0.524208 0.429364 0.50121 0.388012
0.701313 0.542702 0.635594 0.445091 0.474134 0.507738
0.578612 0.600098 0.681073 0.379377 0.376117 0.382739
0.560697 0.464972 0.565296 0.666632 0.555214 0.43695
0.424151 0.417388 0.423629 0.33668 0.3708 0.438755
0.365672 0.478594 0.454445 0.586841 0.819103 0.350521
0.405075 0.290974 0.422474 0.408982 0.40574 0.528026
0.54672 0.479572 0.677429 0.43539 0.542993 0.506841
0.414468 0.322273 0.453274 0.581392 0.406025 0.316664
0.528891 0.80073 0.433797 0.526702 0.3654 0.450929
0.577358 0.542299 0.594244 0.646879 0.559846 0.782022
0.558551 0.336556 0.400094 0.258908 0.391449 0.352934
0.646361 0.353842 0.499265 0.327115 0.549462 0.378965
0.451339 0.626802 0.349635 0.582034 0.413845 0.652098
0.33319 0.481003 0.555634 0.513668 0.330609 0.562775
0.410016 0.604974 0.32622 0.240461 0.723227 0.403213
5) Given an 80x6 matrix A of random numbers, construct an 80x6 matrix B that takes the value 1 in element i,j if element i,j in A takes a value greater than the mean, and -1 if its value is less than or equal to the mean:
using Statistics;
A=rand(80.6)
B=zeros(80.6)
mean=mean(A)
B[A.>mean].=1
B[A.<=mean].=-1
B
6) Given a vector of school grades, compute statistics on passing grades >=6:
votes=[2 8 4 8.1 9.25 11.25 4.75 17 6.25 13 10 2 3.25 3.75 8.5 16 8 1 2.5 12 10.75 6 12 10 11.75 3.5 10.5 8.5 14.25 16.5 10.75 8 12 1 10 13 6.75 5.75 9.5 12.75 11 8.5 10.25 14.5 4.25 5.5 9.75 16.5 13 15 13 13.75 13.5];
println("average=",mean(votes))
println("average sufficient=",mean(votes[votes.>6]))
println("variance sufficient=",var(votes[votes.>6]))
println("sum of sufficient votes=",sum(votes[votes.>6]))
println("number sufficient=",count(i->i>6,votes))
media=9.289622641509434
media sufficienti=11.361538461538462
varianza sufficienti=7.8240080971659935
somma voti sufficienti=443.1
numero sufficienti=39
7) Given a vector, find the Fibonacci numbers in it:
fibo=zeros(Int64,20);
fibo[1]=0;
fibo[2]=1;
for i in 3:20
fibo[i]=fibo[i-1]+fibo[i-2]
end
println(fibo)
a = [13 7 9 34 6 12 43]
a[findall(in(fibo), a)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
2-element Vector{Int64}:
13
34