How to find the location of values ​​in a matrix in Julia?

I used findwith a 3D matrix Aas follows:

julia> find(A.==1)
2-element Array{Int64,1}:
1
234
4567

Julia gives me location as an index, not as an array of indexes. For example, it returns 234 instead of (1,2,1).

I considered this question , but my matrix is ​​very large and has a shape (360,360,360). I can not use the proposed method.

I tried to examine its index pattern and convert it using the function that I encoded:

function cmf_p(matrix)
      for a=1:length(matrix);
          aa=matrix[a]
          rd_u_m=ceil(aa/(360^2))
          rd_d_m=floor(aa/(360^2)-1)
          rd_d_t=(aa-rd_d_m*360)/360^2   
          rd_d_p=aa-rd_d_m*360^2-floor(rd_d_t)*360
          println(rd_u_m);
          println(ceil(rd_d_t)*360);
          println(ceil(aa-rd_d_m*360^2-floor(rd_d_t)*360))
      end    
end

But that gives me the wrong result.

How can I use an index and convert it to the right place?

+3
source share
1 answer

You are looking for ind2sub:

julia> A = eye(3)
3x3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

julia> inds = find(A.==1.0)
3-element Array{Int64,1}:
 1
 5
 9

julia> [ind2sub(size(A), ind) for ind in inds]
3-element Array{Any,1}:
 (1,1)
 (2,2)
 (3,3)
+5

Source: https://habr.com/ru/post/1665396/


All Articles