Searching for number series in matrix with numgo

xuanbao · · 59 次点击    
<p>Hey there,</p> <p>pretty new to golang and discovered gonum a couple of days ago - awesome piece of software.</p> <p>I&#39;m currently trying to find a series (or progression, don&#39;t know how to call it in english) of numbers in a matrix.</p> <p>Case is:</p> <p>1 | 0 | 1 | 1 | 1 | 1 | 0</p> <p>0 | 1 | 0 | 1 | 1 | 0 | 1</p> <p>1 | 0 | 1 | 1 | 0 | 1 | 0</p> <p>1 | 0 | 0 | 1 | 1 | 1 | 0</p> <p>1 | 0 | 0 | 1 | 0 | 1 | 0</p> <p>Values are ones or zeroes (might be other numeric values in the future). The dimensions of the matrix are growing really heave (might be up to 95x365 or even more in the future).</p> <p>So, is there an intelligent/efficient solution to find a series of, let&#39;s say, 4 ones (other than just &#34;bruteforcing&#34; it)? Let&#39;s there the row is 0 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 and i want the two occurrences of 4 ones in a row (the first index is enough i guess).</p> <p>Bonus would be to also take into consideration row overlaps. So a series of 3 ones in the very last columns of the row and 1 one at the very beginning of the next row.</p> <p>I&#39;m not really good and up to date when it comes to matrices... it&#39;s been some time since I heard about it in school :-D.</p> <p>Would be very awesome to get some input from you guys :).</p> <hr/>**评论:**<br/><br/>chewxy: <pre><p>if you mean <code>gonum</code>, then just get the <code>.Data</code> field, and iterate manually through that.</p> <p>Here&#39;s the equivalent you can do with <a href="https://github.com/chewxy/gorgonia" rel="nofollow">gorgonia/tensor</a></p> <pre><code>import T &#34;github.com/chewxy/gorgonia/tensor&#34; mat := T.New(T.WithShape(95,356), T.Of(T.Byte)) dat := mat.Data().([]byte) // will return []byte var count int for i := range dat { if dat[i] == 1 { count++ } if count == THRESHOLD { coord, _ := T.Itol(i-THRESHOLD, mat.Shape(), mat.Strides()) log.Printf(&#34;Start of series found at %v&#34;, coord) } } </code></pre> <p>You can do a similar iteration using Gonum, but you&#39;d have to write your own <code>Itol</code> function</p></pre>fgreinus: <pre><p>Yes, I wanted to write gonum :-D.</p> <p>Thanks! Will have a look into that.</p></pre>chewxy: <pre><p>If you only ever need float64 matrices, then use gonum, don&#39;t use <code>tensor</code>. </p></pre>fgreinus: <pre><p>No, really tiny numbers only. Thanks for the hint :)</p></pre>chewxy: <pre><p>then package <a href="https://github.com/chewxy/gorgonia" rel="nofollow"><code>tensor</code></a> might be more your thing. It&#39;s a generic multidimensional array package. Might be a bit heavy for your usecase tho</p></pre>
59 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传