Question

Ok, so I am still getting stumped by basic syntax. Right now I would like to know how to create a matrix filled with a single value c and once created how to replace an entire row. So far this is what I have:

c = 5
nrow = 6
ncol = 4
parm = [c for i=1:nrow, j=1:ncol]
parm[5, 1:end] = 0

parm
6x4 Array{Any,2}:
 5  5  5  5
 5  5  5  5
 5  5  5  5
 5  5  5  5
 0  0  0  0
 5  5  5  5

The above syntax works but seems unnecessarily verbose. Any suggestions?

Thanks, Francis

Was it helpful?

Solution

You can use the fill function to construct an array filled with a specific value:

julia> A = fill(5,(6,4))
6x4 Array{Int64,2}:
 5  5  5  5
 5  5  5  5
 5  5  5  5
 5  5  5  5
 5  5  5  5
 5  5  5  5

julia> A[5,:] = 0
0

julia> A
6x4 Array{Int64,2}:
 5  5  5  5
 5  5  5  5
 5  5  5  5
 5  5  5  5
 0  0  0  0
 5  5  5  5

You also don't need to write 1:end – you can just write : for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top