<small id='FQYqN'></small><noframes id='FQYqN'>

  • <i id='FQYqN'><tr id='FQYqN'><dt id='FQYqN'><q id='FQYqN'><span id='FQYqN'><b id='FQYqN'><form id='FQYqN'><ins id='FQYqN'></ins><ul id='FQYqN'></ul><sub id='FQYqN'></sub></form><legend id='FQYqN'></legend><bdo id='FQYqN'><pre id='FQYqN'><center id='FQYqN'></center></pre></bdo></b><th id='FQYqN'></th></span></q></dt></tr></i><div id='FQYqN'><tfoot id='FQYqN'></tfoot><dl id='FQYqN'><fieldset id='FQYqN'></fieldset></dl></div>
          <bdo id='FQYqN'></bdo><ul id='FQYqN'></ul>
      1. <legend id='FQYqN'><style id='FQYqN'><dir id='FQYqN'><q id='FQYqN'></q></dir></style></legend>
        <tfoot id='FQYqN'></tfoot>

        如何使用 Julia 在矩阵中查找连通分量

        How to find connected components in a matrix using Julia(如何使用 Julia 在矩阵中查找连通分量)

        <tfoot id='p5Pqg'></tfoot>

            <i id='p5Pqg'><tr id='p5Pqg'><dt id='p5Pqg'><q id='p5Pqg'><span id='p5Pqg'><b id='p5Pqg'><form id='p5Pqg'><ins id='p5Pqg'></ins><ul id='p5Pqg'></ul><sub id='p5Pqg'></sub></form><legend id='p5Pqg'></legend><bdo id='p5Pqg'><pre id='p5Pqg'><center id='p5Pqg'></center></pre></bdo></b><th id='p5Pqg'></th></span></q></dt></tr></i><div id='p5Pqg'><tfoot id='p5Pqg'></tfoot><dl id='p5Pqg'><fieldset id='p5Pqg'></fieldset></dl></div>

            <small id='p5Pqg'></small><noframes id='p5Pqg'>

                  <bdo id='p5Pqg'></bdo><ul id='p5Pqg'></ul>
                  <legend id='p5Pqg'><style id='p5Pqg'><dir id='p5Pqg'><q id='p5Pqg'></q></dir></style></legend>
                    <tbody id='p5Pqg'></tbody>
                  本文介绍了如何使用 Julia 在矩阵中查找连通分量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Assume I have the following matrix (defined here in Julia language):

                      mat = [1 1 0 0 0 ; 1 1 0 0 0 ; 0 0 0 0 1 ; 0 0 0 1 1]
                  

                  Considering as a "component" a group of neighbour elements that have value '1', how to identify that this matrix has 2 components and which vertices compose each one?

                  For the matrix mat above I would like to find the following result:

                  Component 1 is composed by the following elements of the matrix (row,column):

                      (1,1)
                      (1,2)
                      (2,1)
                      (2,2)
                  

                  Component 2 is composed by the following elements:

                      (3,5)
                      (4,4)
                      (4,5)
                  

                  I can use Graph algorithms like this to identify components in square matrices. However such algorithms can not be used for non-square matrices like the one I present here.

                  Any idea will be much appreciated.

                  I am open if your suggestion involves the use of a Python library + PyCall. Although I would prefer to use a pure Julia solution.

                  Regards

                  解决方案

                  Using Image.jl's label_components is indeed the easiest way to solve the core problem. However, your loop over 1:maximum(labels) may not be efficient: it's O(N*n), where N is the number of elements in labels and n the maximum, because you visit each element of labels n times.

                  You'd be much better off just visiting each element of labels just twice: once to determine the maximum, and once to assign each non-zero element to its proper group:

                  using Images
                  
                  function collect_groups(labels)
                      groups = [Int[] for i = 1:maximum(labels)]
                      for (i,l) in enumerate(labels)
                          if l != 0
                              push!(groups[l], i)
                          end
                      end
                      groups
                  end
                  
                  mat = [1 1 0 0 0 ; 1 1 0 0 0 ; 0 0 0 0 1 ; 0 0 0 1 1]
                  
                  labels = label_components(mat)
                  groups = collect_groups(labels)
                  

                  Output on your test matrix:

                  2-element Array{Array{Int64,1},1}:
                   [1,2,5,6] 
                   [16,19,20]
                  

                  Calling library functions like find can occasionally be useful, but it's also a habit from slower languages that's worth leaving behind. In julia, you can write your own loops and they will be fast; better yet, often the resulting algorithm is much easier to understand. collect(zip(ind2sub(size(mat),find( x -> x == value, mat))...)) does not exactly roll off the tongue.

                  这篇关于如何使用 Julia 在矩阵中查找连通分量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                    <i id='1lyjb'><tr id='1lyjb'><dt id='1lyjb'><q id='1lyjb'><span id='1lyjb'><b id='1lyjb'><form id='1lyjb'><ins id='1lyjb'></ins><ul id='1lyjb'></ul><sub id='1lyjb'></sub></form><legend id='1lyjb'></legend><bdo id='1lyjb'><pre id='1lyjb'><center id='1lyjb'></center></pre></bdo></b><th id='1lyjb'></th></span></q></dt></tr></i><div id='1lyjb'><tfoot id='1lyjb'></tfoot><dl id='1lyjb'><fieldset id='1lyjb'></fieldset></dl></div>

                  1. <legend id='1lyjb'><style id='1lyjb'><dir id='1lyjb'><q id='1lyjb'></q></dir></style></legend>
                      <bdo id='1lyjb'></bdo><ul id='1lyjb'></ul>

                          <tbody id='1lyjb'></tbody>
                        <tfoot id='1lyjb'></tfoot>
                          • <small id='1lyjb'></small><noframes id='1lyjb'>