Matlab中是否有根据条件连接数组的函数?

huangapple go评论76阅读模式
英文:

Is there any function for matlab to concantenate array using condition?

问题

我有这段Python代码,用多个条件来连接两个数组,如下所示:

good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & 
    (nonzerox >= win_xleft_low) &  (nonzerox < win_xleft_high)).nonzero()

是否有MatLab等效的代码可以处理这种类型的数组?

英文:

I have this python code for concatenating 2 arrays using multiple conditions like give below

good_left_inds = ((nonzeroy &gt;= win_y_low) &amp; (nonzeroy &lt; win_y_high) &amp; 
    (nonzerox &gt;= win_xleft_low) &amp;  (nonzerox &lt; win_xleft_high)).nonzero()

is there any MatLab equivalent code to work on this kind of array?

答案1

得分: 1

是的,有的。请查看MATLAB的find函数。其文档说

> 查找非零元素的索引和值

这基本上与numpy.nonzero做的事情相同。逻辑表达式在实际上与Python代码几乎相同。

<!-- language: matlab -->

% 您只对索引感兴趣,因此可以省略返回参数中的值
[good_left_inds,〜] = find(nonzerox>=win_y_low&nonzerox<win_y_high&...
nonzerox>=win_xleft_low&nonzerox<win_xleft_high);

在比较结果时,请记住MATLAB的索引从1开始,而不是像Python中的0

英文:

Yes, there is. Have a look at MATLABs find function. Its documentation says

> Find indices and values of nonzero elements

It is basically the same what numpy.nonzero does. The logical expressions are practically identical to python code.

<!-- language: matlab -->

% You are only interested in the indices, so you can omit the values as return parameter
[good_left_inds, ~] = find(nonzerox &gt;= win_y_low &amp; nonzerox &lt; win_y_high &amp; ...
    nonzerox &gt;= win_xleft_low &amp; nonzerox &lt; win_xleft_high);

When comparing the results, please remember that MATLAB indexing starts at 1 and not at 0 like in python.

huangapple
  • 本文由 发表于 2020年1月6日 16:44:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定