Reputation: 45
I'm working with a 121x137 (i,j) array (mortality table) and am trying to create a 121x1 vector from this made up of the (i+1,j+1) values (i.e. index (50,50), (51,51) and so forth.
I'm using the following code - BaseTable is my 121x137 array:
age=50
survivalcurve = for i in age:nrow(BaseTable)-1
for j in age:ncol(BaseTable)-1
println(getindex(FemaleBaseTable, i+1, j+1))
end
end
However, when I do this, its returning all values of i and j - if I picture the values I want as a diagonal line running top-bottom, L-R of my table, its giving me all the values on the top right of my imaginary diagonal line.
If I fix i and loop through j, it works and returns the entire 50th row:
age=50
survivalcurve =
for j in age:ncol(FemaleBaseTable)
println(getindex(FemaleBaseTable, 50, j+1))
end
and likewise if I fix j and loop through i, this works and returns the entire jth column:
age=50
survivalcurve =
for i in age:nrow(FemaleBaseTable)-1
println(getindex(FemaleBaseTable, i+1, 50))
end
I've surmised its returning all values because I'm using age:nrow / age:ncol, but not sure what a suitable replacement would be to only return the (i+1),(j+1) value would be. Any help would be appreciated!
Upvotes: 0
Views: 131
Reputation: 5559
You can collect the diagonal elements starting at any index (i,j)
by using either diag
from LinearAlgebra.jl
or manually using a single loop.
Assuming you have:
nrows, ncols = 121, 137
FemaleBaseTable = rand(nrows, ncols)
# And the diagonal should start at any (i,j)
i, j = 50, 50
You can either use
diag(@view FemaleBaseTable[i:end,j:end])
or,
[FemaleBaseTable[i,i] for (i,j) in zip(i:nrows,j:ncols)]
Upvotes: 2