如何在Matlab中最有效地比较坐标对而不使用嵌套循环? [英] How to compare the pairs of coordinates most efficiently without using nested loops in Matlab?

查看:236
本文介绍了如何在Matlab中最有效地比较坐标对而不使用嵌套循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有20对坐标,其x和y值为:

If I have 20 pairs of coordinates, whose x and y values are say :

x   y
27  182
180 81
154 52
183 24
124 168
146 11
16  90
184 153
138 133
122 79
192 183
39  25
194 63
129 107
115 161
33  14
47  65
65  2
1   124
93  79

现在如果我随机生成15对坐标(x,y)并且想要与上面给出的这20对坐标进行比较,我如何在没有嵌套循环的情况下最有效地进行?

Now if I randomly generate 15 pairs of coordinates (x,y) and want to compare with these 20 pairs of coordinates given above, how can I do that most efficiently without nested loops?

推荐答案

如果你想看看你的15个随机生成的坐标对是否等于任何你的20个原始坐标对,一个简单的解决方案是使用函数 ISMEMBER 如下:

If you're trying to see if any of your 15 randomly generated coordinate pairs are equal to any of your 20 original coordinate pairs, an easy solution is to use the function ISMEMBER like so:

oldPts = [...];  %# A 20-by-2 matrix with x values in column 1
                 %#   and y values in column 2
newPts = randi(200,[15 2]);  %# Create a 15-by-2 matrix of random
                             %#   values from 1 to 200
isRepeated = ismember(newPts,oldPts,'rows');

isRepeated -1逻辑数组,其中在 oldPts 中存在 newPts 的行,否则为零。

And isRepeated will be a 15-by-1 logical array with ones where a row of newPts exists in oldPts and zeroes otherwise.

这篇关于如何在Matlab中最有效地比较坐标对而不使用嵌套循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆