BoundsError

I have created a function as

function ShortestPath(DAG, K2Score)

for i = 1:length(K2Score)
ind = findall(K2Score .== 0)
#K2Score[1,ind] ~= root,
K2Score[1,ind] = 0.01

end

Finding the root.

G = DAG
M,N = size[DAG]
k = 1
for i in 1:M
for j in 1:N
if G[i,j] ~= 0
a[1,k] = i
a[2,k] = j
k = k +1
end
end
end

n = k-1
k = 1;
for j in 1:N
flag = 0
for i in 1:M
if G[i,j] ~= 0
flag = 1
end
end
if flag .== 0
root[1,k] = j; # Must store all the values
k = k+1
end
end

Applying bellman algorithm

for ii = 1:length(root)

k =1
for i = 1:n
if a[1,i] .== root[1,ii]
b[1,k] = a[1,i]
b[2,k] = a[2,i]
k = k+1
end
end

for i = 1:n
if a[1,i] ~= root[1,ii]
b[1,k] = a[1,i]
b[2,k] = a[2,i]
k = k+1
end
end

for i = 1:M
for ii = 1:length(root)
dist[ii,i] = 20000; # infinity Value
end
end

dist[ii,root[1,ii]] = 0
for i = 1: M
for j = 1:n
k = dist[ii,b[1,j]] + 1
if k .< dist[ii,b[2,j]]
dist[ii,b[2,j]] = k
end
end
end

Finding the minimum of distance

final_dist = minimum(dist, dims = 1)

end

return final_dist

end

And I am us

final_dist_UCII_WOMEN = ShortestPath(UCIV_WOMEN_DAG, UCIV_WOMEN_K2Score)

Throughs an error like

``````BoundsError: attempt to access 1×112 Array{Float64,2} at index [1, CartesianIndex{2}[CartesianIndex(1, 79), CartesianIndex(1, 82), CartesianIndex(1, 83), CartesianIndex(1, 85), CartesianIndex(1, 88), CartesianIndex(1, 91), CartesianIndex(1, 94), CartesianIndex(1, 97), CartesianIndex(1, 104)]]

Stacktrace:
[1] throw_boundserror(::Array{Float64,2}, ::Tuple{Int64,Array{CartesianIndex{2},1}}) at .\abstractarray.jl:541
[2] checkbounds at .\abstractarray.jl:506 [inlined]
[3] _setindex! at .\multidimensional.jl:784 [inlined]
[4] setindex! at .\abstractarray.jl:1153 [inlined]
[5] ShortestPath(::Array{Float64,2}, ::Array{Float64,2}) at .\In[33]:6
[6] top-level scope at In[34]:1

``````

The data in UCIV_WOMEN_K2Score is like

``````Tables.MatrixTable{Array{Float64,2}}:
:Column1    [-282.75936567503123]
:Column2    [-305.58336610157846]
:Column3    [-292.41728385977933]
:Column4    [-305.18462549235426]
:Column5    [-274.38053518866104]
:Column6    [-264.9541448871945]
:Column7    [-220.70659792617909]
:Column8    [-134.11407429858826]
:Column9    [-238.25801615743833]
:Column10   [-223.2858117418622]
:Column11   [-297.1694634952932]
:Column12   [-296.66690599815234]
:Column13   [-280.3308279437817]
:Column14   [-232.9550950909364]
:Column15   [-232.13675627716506]
:Column16   [-236.38528000947178]
:Column17   [-261.930952318306]
:Column18   [-260.73925958765756]
:Column19   [-289.29985505843]
:Column20   [-248.3846110642878]
:Column21   [-265.17573796201276]
:Column22   [-304.0344529707333]
:Column23   [-289.8263398616875]
:Column24   [-257.0691832080685]
:Column25   [-291.3757440508586]
:Column26   [-264.0948838305343]
:Column27   [-292.5600596311132]
:Column28   [-249.35945573391615]
:Column29   [-81.92327266391918]
:Column30   [-119.47535227825449]
:Column31   [-290.87671876102786]
:Column32   [-259.1160299476976]
:Column33   [-274.5405597958887]
:Column34   [-282.42420745421214]
:Column35   [-250.5227823754762]
:Column36   [-237.45806324419493]
:Column37   [-311.6203721601196]
:Column38   [-284.93117212740185]
:Column39   [-273.39518795019046]
:Column40   [-217.78300320183075]
:Column41   [-251.84915951112367]
:Column42   [-272.9138143613708]
:Column43   [-287.4156101302241]
:Column44   [-114.37471605094095]
:Column45   [-161.41934549656662]
:Column46   [-261.84687097011204]
:Column47   [-274.9514937520016]
:Column48   [-158.65000549478196]
:Column49   [-109.86505012401156]
:Column50   [-213.1359440025262]
:Column51   [-75.29153514406694]
:Column52   [-195.70559894473286]
:Column53   [-213.0431581230352]
:Column54   [-281.5760210196869]
:Column55   [-295.204849892014]
:Column56   [-8.409162447202561]
:Column57   [-129.96265278868083]
:Column58   [-199.24228981544687]
:Column59   [-279.023929653169]
:Column60   [-170.92260998595782]
:Column61   [-96.28484854268876]
:Column62   [-84.50709805496444]
:Column63   [-28.22050948456115]
:Column64   [-5.105945473900647]
:Column65   [-46.82916759238583]
:Column66   [-8.409162447202561]
:Column67   [-8.409162447202561]
:Column68   [-8.409162447202561]
:Column69   [-8.409162447202561]
:Column70   [-8.409162447202561]
:Column71   [-15.468565275793821]
:Column72   [-8.409162447202561]
:Column73   [-8.409162447202561]
:Column74   [-8.409162447202561]
:Column75   [-8.409162447202561]
:Column76   [-13.299511575424276]
:Column77   [-8.409162447202561]
:Column78   [-8.409162447202561]
:Column79   [0.0]
:Column80   [-8.409162447202561]
:Column81   [-8.409162447202561]
:Column82   [0.0]
:Column83   [0.0]
:Column84   [-8.409162447202561]
:Column85   [0.0]
:Column86   [-8.409162447202561]
:Column87   [-8.409162447202561]
:Column88   [0.0]
:Column89   [-8.409162447202561]
:Column90   [-8.409162447202561]
:Column91   [0.0]
:Column92   [-15.468565275793821]
:Column93   [-15.468565275793821]
:Column94   [0.0]
:Column95   [-8.409162447202561]
:Column96   [-8.409162447202561]
:Column97   [0.0]
:Column98   [-5.105945473900647]
:Column99   [-5.105945473900647]
:Column100  [-5.105945473900647]
:Column101  [-5.105945473900647]
:Column102  [-15.468565275793821]
:Column103  [-5.105945473900647]
:Column104  [0.0]
:Column105  [-8.409162447202561]
:Column106  [-15.468565275793821]
:Column107  [-5.105945473900647]
:Column108  [-5.105945473900647]
:Column109  [-5.105945473900647]
:Column110  [-5.105945473900647]
:Column111  [-8.409162447202561]
:Column112  [-15.468565275793821]

``````

Thanks

Hi @ashwanimalviya, welcome to our community!

I recommend you look at our list of good practices. I think you used the blockquote instead of triple backticks to post your code and it lost its identation, making it harder to read. If you use triple backticks instead the spacing is preserved (and a monospace font is used).

If you break your code in smaller functions the error message will probably be more precise. For now it is hard to understand which exact line of your problem that is throwing the error.

Giving your code a look, I found strange that you have `M, N = size[DAG]`, did you mean `M, N = size(DAG)`? Do you have some data to test you function with that throws the same error?

2 Likes

I am reinserting the original code

``````function ShortestPath(DAG, K2Score)

for i = 1:length(K2Score)
ind = findall(K2Score .== 0.0)
#K2Score[1,ind] ~= root,
K2Score[1,ind] = 0.01

end

#  Finding the root.
G = DAG
M,N = size(DAG)
k = 1
for i in 1:M
for j in 1:N
if G[i,j] ~= 0
a[1,k] = i
a[2,k] = j
k = k +1
end
end
end

##
n = k-1
k = 1;
for j in 1:N
flag = 0
for i in 1:M
if G[i,j] ~= 0
flag = 1
end
end
if flag .== 0
root[1,k] = j; # Must store all the values
k = k+1
end
end

## Applying bellman algorithm

for ii = 1:length(root)

k =1
for i = 1:n
if a[1,i] .== root[1,ii]
b[1,k] = a[1,i]
b[2,k] = a[2,i]
k = k+1
end
end

for i = 1:n
if a[1,i] ~= root[1,ii]
b[1,k] = a[1,i]
b[2,k] = a[2,i]
k = k+1
end
end

for i = 1:M
for ii = 1:length(root)
dist[ii,i] = 20000; # infinity Value
end
end

dist[ii,root[1,ii]] = 0
for i = 1: M
for j = 1:n
k = dist[ii,b[1,j]] + 1
if k .< dist[ii,b[2,j]]
dist[ii,b[2,j]] = k
end
end
end
## Finding the minimum of distance
final_dist = minimum(dist, dims = 1)

end

return final_dist

end

``````

can you please suggest me the way to attached the input data .csv file as per your request to test the function.
Thank you very much for taking your time.

2 Likes

I think you can use https://pastebin.com/ for the CSV data.

How do I send you the data on pastebin?

Paste the code here, press “Create new paste”, and copy here the link of the page it will redirect you.

Hi @Henrique_Becker , I have mailed you the data. Please give your suggestion here as you feel right.
Thanks

@ashwanimalviya, I did not have time to dive into your problem until now.

``````using CSV, DataFrames

ShortestPath(dag, k2s)
``````

This already fails at the start, with:

``````ERROR: MethodError: no method matching length(::DataFrame)
``````

What data structures are your code expecting? How do you read them from the CSV?

The function “ShortestPath” is expecting the inputs(dag, k2s) in matrix form.
Since these inputs are also an output from different function and needs to be feed as input for ShortestPath function.

I hope you get my point.
Please feel free to write if you need to know anything about it further.

Thanks
Ashwani

Ashwani,

I read the CSVs in matrix form, and started trying to put it to work, but the problems kept appearing:

1. The problem you report was easy to fix. It is just that the `ind` returned by `findall` already gives you a list of `CartesianIndex{2}`, so when you use it to index K2Score, you should not do `K2Score[1,ind]` but instead just `K2Score[ind]`.
2. After this, I found the problem that `~=` is not defined. I suppose you meant `isapprox`. However, `isapprox` needs an absolute tolerance to compare with zero (what seems to be your intent), so I guessed `0.01` was a good enough tolerance, but you may need to change this.
3. Finally, you refer to some `a` variables in your code, but your code does not define them. I had nothing to do here, as I do not know anything about them. At this point I stopped.

The code I changed is here.

2 Likes

Thanks for your time taken to look into the matter.
Following your suggestion to break the code into smaller functions, I have recreated the code to solve the whole task into smaller tasks. The first and second point you have mentioned is solved in this way. Thanks.

I would like to mention that `~=` is for comparing `not equal to` in MATLAB. I have used `!=` in Julia and it is working fine.

Now, the next thing is, I want to create a matrix `a` of 2 rows and variable columns out of `DAG`

``````# creating vector a out of DAG
function Creat_Vector_a(DAG)
G = DAG
M,N = size(DAG)
a1 = reshape(Int64[],0,1)
a2 = reshape(Int64[],0,1)
k = 1
for i in 1:M
for j in 1:N
if G[i,j]!= 0'
a1[k] = i
a2[k] = j
k = k +1
if k > N
break
end
a = [a1; a2]

end
end
end

return a

end

``````

I am getting the following error

``````BoundsError: attempt to access 0×1 Array{Int64,2} at index [1]

Stacktrace:
[1] setindex! at .\array.jl:847 [inlined]
[2] Creat_Vector_a(::Array{Float64,2}) at .\In[87]:13
[3] top-level scope at In[88]:1
``````

Thanks

The problems is that both `a1` and `a2` have no valid positions (as they have zero rows), so you cannot ever save anything inside them.

Did you read the documentation for `reshape`? What exactly you intend to do with a matrix that has zero rows (and one column)? Also, why would you `reshape` a just-created `Int64[]`, you do reshape to make already allocated data take another shape. If you want to allocate a matrix do it directly, i.e., `Matrix{Int64}(undef, number_of_rows, number_of_columns)` if you want uninitialized data, or `zeros(Int64, number_of_rows, number_of_columns)` if you want zeroed data.

Thank for the correction. The code worked.
But I am not getting the desired result.

form the previous code, I want to search in every column of `DAG` starting from `1 to N` and if `0` is not found, I want to store the index `i in a1` and `j in a2` and finally a matrix `a`.

for example,
If there are two non zero values in column 1 of `DAG` at rows 3 and 84, then it must give me `a1[1,1] = 1, a2[1,1] = 3 and a1[1,2] = 1, a2[1,2] = 84`

in this way our matrix `a` must look like

``````a[1,1] = 1, a[1,2] = 1
a[2,1] = 3, a[2,2] = 84
and so on
``````

Instead of getting this result, I am getting

`````` :Column1    [1.0, 1.0]
:Column2    [1.0, 2.0]
:Column3    [1.0, 3.0]
:Column4    [1.0, 5.0]
:Column5    [1.0, 6.0]
:Column6    [1.0, 7.0]
:Column7    [1.0, 8.0]
:Column8    [1.0, 9.0]
:Column9    [1.0, 10.0]
:Column10   [1.0, 11.0]
:Column11   [1.0, 12.0]
:Column12   [1.0, 13.0]
:Column13   [1.0, 14.0]
:Column14   [1.0, 15.0]
:Column15   [1.0, 16.0]
:Column16   [1.0, 17.0]
:Column17   [1.0, 18.0]
:Column18   [1.0, 19.0]
:Column19   [1.0, 20.0]
:Column20   [1.0, 21.0]
:Column21   [1.0, 22.0]
:Column22   [1.0, 23.0]
:Column23   [1.0, 24.0]
:Column24   [1.0, 25.0]
:Column25   [1.0, 26.0]
:Column26   [1.0, 27.0]
:Column27   [1.0, 28.0]
:Column28   [1.0, 29.0]
:Column29   [1.0, 30.0]
:Column30   [1.0, 31.0]
:Column31   [1.0, 32.0]
:Column32   [1.0, 33.0]
:Column33   [1.0, 34.0]
:Column34   [1.0, 35.0]
:Column35   [1.0, 36.0]
:Column36   [1.0, 37.0]
:Column37   [1.0, 38.0]
:Column38   [1.0, 39.0]
:Column39   [1.0, 40.0]
:Column40   [1.0, 41.0]
:Column41   [1.0, 42.0]
:Column42   [1.0, 43.0]
:Column43   [1.0, 44.0]
:Column44   [1.0, 45.0]
:Column45   [1.0, 46.0]
:Column46   [1.0, 47.0]
:Column47   [1.0, 48.0]
:Column48   [1.0, 49.0]
:Column49   [1.0, 50.0]
:Column50   [1.0, 51.0]
:Column51   [1.0, 52.0]
:Column52   [1.0, 53.0]
:Column53   [1.0, 54.0]
:Column54   [1.0, 55.0]
:Column55   [1.0, 56.0]
:Column56   [1.0, 57.0]
:Column57   [1.0, 58.0]
:Column58   [1.0, 59.0]
:Column59   [1.0, 60.0]
:Column60   [1.0, 61.0]
:Column61   [1.0, 62.0]
:Column62   [1.0, 63.0]
:Column63   [1.0, 64.0]
:Column64   [1.0, 65.0]
:Column65   [1.0, 66.0]
:Column66   [1.0, 67.0]
:Column67   [1.0, 68.0]
:Column68   [1.0, 69.0]
:Column69   [1.0, 70.0]
:Column70   [1.0, 71.0]
:Column71   [1.0, 72.0]
:Column72   [1.0, 73.0]
:Column73   [1.0, 74.0]
:Column74   [1.0, 75.0]
:Column75   [1.0, 76.0]
:Column76   [1.0, 77.0]
:Column77   [1.0, 78.0]
:Column78   [1.0, 79.0]
:Column79   [1.0, 80.0]
:Column80   [1.0, 81.0]
:Column81   [1.0, 82.0]
:Column82   [1.0, 83.0]
:Column83   [1.0, 84.0]
:Column84   [1.0, 85.0]
:Column85   [1.0, 86.0]
:Column86   [1.0, 87.0]
:Column87   [1.0, 88.0]
:Column88   [1.0, 89.0]
:Column89   [1.0, 90.0]
:Column90   [1.0, 91.0]
:Column91   [1.0, 92.0]
:Column92   [1.0, 93.0]
:Column93   [1.0, 94.0]
:Column94   [1.0, 95.0]
:Column95   [1.0, 96.0]
:Column96   [1.0, 97.0]
:Column97   [1.0, 98.0]
:Column98   [1.0, 99.0]
:Column99   [1.0, 100.0]
:Column100  [1.0, 101.0]
:Column101  [1.0, 102.0]
:Column102  [1.0, 103.0]
:Column103  [1.0, 104.0]
:Column104  [1.0, 105.0]
:Column105  [1.0, 106.0]
:Column106  [1.0, 107.0]
:Column107  [1.0, 108.0]
:Column108  [1.0, 109.0]
:Column109  [1.0, 110.0]
:Column110  [1.0, 111.0]
:Column111  [1.0, 112.0]
:Column112  [2.0, 2.0]
``````

Please let me know where am I making mistake in the code.

Thanks

I have updated the code like below

``````# creating vector a out of DAG
function Creat_Vector_a(DAG)
G = DAG
M,N = size(DAG)
a1 = Matrix{Int64}(undef, 1, 2000)
a2 = Matrix{Int64}(undef, 1, 2000)
#b = zeros(2,N)
k = 1
for i in 1:M
for j in 1:N
if G[i,j]!== 0
a1[1,k] = i
#push!(a1[k], i)
a2[1,k] = j
#push!(a2[k], j)
k = k +1
if k > N
break
end
a = [a1; a2]

end
end
end

return a

end

``````

Why `a1` and `a2` are matrices, or better, why `a1` and `a2` have both rows and columns, in the example you only change the second index, the first index will change in some situation? They are not better as `Vectors`? In fact, in your code you save them by using:

``````           a1[k] = i
a2[k] = j
``````

In other words, you only use one index, it seems to me that they are Vectors that for some reason you want to treat as Matrices. As you do not know the size of the vectors before starting you could define them as:

``````a1 = Int[]
a2 = Int[]
``````

and then when you fill them you could instead

``````push!(a1, i)
push!(a2, j)
``````

There is some reason to why

``````                if k > N
break
end
``````

? You do want to limit the number of nonzero entries obtained?

Also, you are making `a` with `a = [a1; a2]` every time you finda a non-zero, why? Do you not want to do this only at the end, and as they would be vectors it would be written this way:

``````a = [a1'; a2']
``````

I followed your suggestion and updated the code as

``````function Creat_Vector_a(DAG)
G = DAG
M,N = size(DAG)
a1 = Int[]
a2 = Int[]
k = 1
for i in 1:M
for j in 1:N
if G[i,j]!== 0
a1[k] = i
a2[k] = j
push!(a1, i)
push!(a2, j)
k = k +1
end
a = [a1'; a2']
end
end

return a

end

``````

I am getting the following error now

``````BoundsError: attempt to access 0-element Array{Int64,1} at index [1]

Stacktrace:
[1] setindex! at .\array.jl:847 [inlined]
[2] Creat_Vector_a(::Array{Float64,2}) at .\In[167]:12
[3] top-level scope at In[168]:1
The error happens because you are trying to save an element to position `1` when the `Vector` has zero positions (it is empty). Maybe in other languages the position would be created for you but this is not the case in Julia. My suggestion was that you should use `push!` instead of the attribution (i.e., `a1[k] = i` and `a2[k] = j`), so keep only the two last lines of these four. `push!` will create a new position for you (after all current positions), and save the value you passed in such position. Also, you do not need `k` anymore, because `push!` will always add the new element after the last one so you do not need to control the current position.
Finally, did you change the position of `a = [a1'; a2']`? It keeps making a lot of unnecessary effort where it is in your last code. My suggestion would be creating only in the line just before the `return a` (i.e., after closing the two loops).