MATLAB:比较两个数组的所有元素
我在 MATLAB 中有两个矩阵,分别是 arr1
和 arr2
,大小分别为 1000*1000。我想比较它们的元素并将比较结果保存在结果矩阵 resarr
中,该矩阵也是 1000*1000,这样对于每个元素:
- 如果
arr1
中的元素大于一个在arr2
中,如果arr2
中的元素更大,则将值1放入结果中, - 存储值2
,但我不想用for循环来做到这一点,因为那比较慢。我该怎么做?
编辑: 另外,如果我想在 1000*1000*3 结果矩阵中存储不同的 RGB 值,具体取决于 arr1 和 arr2 的比较,是否可以在没有慢循环的情况下完成?
例如,如果 arr1
较大,则存储 (255,0,0);如果 arr2
较大,则存储 (0,255,0)
I have two matrices in MATLAB lets say arr1
and arr2
of size 1000*1000 each. I want to compare their elements and save the comparison in a result matrix resarr
which is also 1000*1000 such that for each element:
- if the element in
arr1
is bigger than the one inarr2
, place the value 1 in the result - if the element in
arr2
is bigger, store the value 2
but I don't want to do this with for loops because that is slower. How can I do this?
EDIT:
Also if I wanted to store different RGB values in a 1000*1000*3 result matrix, depending on the comparison of arr1
and arr2
, could that be done without slow loops?
For example store (255,0,0) if arr1
is larger and (0,255,0) if arr2
is larger
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
arr1>arr2
逐个元素比较 arr1 和 arr2,返回 1000x1000 矩阵,其中 arr1 较大的情况下包含 1,否则返回 0。2 -
部分将其放入矩阵中,如果 arr1 大于 arr2,则为 1,否则为 2。注意:如果 arr1 和 arr2 在某个时刻相等,您也会得到 2(因为 arr1>arr2 返回 0,则 2-0=2)。
arr1>arr2
compares arr1 and arr2, element by element, returning 1000x1000 matrix containing 1 where arr1 is larger, and 0 otherwise. the2 -
part makes it into a matrix where there are 1's if arr1 was larger than arr2, and 2's otherwise.note: if arr1 and arr2 are euqal at some point, you'll also get 2 (because arr1>arr2 return 0, then 2-0=2).
关于您的编辑,一旦您将
resarr
矩阵计算为 Ofri 建议,您可以通过以下方式修改 RGB 矩阵img
:With respect to your edit, once you have your
resarr
matrix computed as Ofri suggested, you can modify an RGB matriximg
in the following way: