使用逻辑索引而不是FIND [英] Use logical indexing instead of FIND

查看:1027
本文介绍了使用逻辑索引而不是FIND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中的一个循环中,我使用单线查找并绘制一些势的最小值(为清楚起见:7对应于包含电位值的单元格,5对应于x值):

Within a loop in my code I use a one-liner to find and plot the minimum of some potential (for clarity: the 7 corresponds to the cells containing the potential values, the 5 to the x-values):

plot(PDdata{subject,5}{1,1}(find(PDdata{subject,7}==...
    min(PDdata{subject,7}))),min(PDdata{subject,7}),'ko')

现在,Matlab建议使用逻辑索引而不是FIND,尽管我只是简要地进行了研究,但它并没有让我感到震惊,因为我应该在这里做些什么.因此,这里的主要问题是,我是否应该采用逻辑索引(我宁愿保持单行格式!),如果是这样:如何?

Now Matlab suggests to use logical indexing instead of FIND and although I've looked into it only briefly, it doesn't strike me as something I should do here. Main question here is thus whether I should employ logical indexing (I prefer to keep it a one-liner!), and if so: how?

对于这样一个小问题,我事先表示歉意,但是我正努力提高我的Matlab知识,因此希望简短的回答已经对我有所帮助!

I apologize in advance for asking such a minor question, but I'm trying to increase my Matlab knowledge so hopefully a short answer would help me out already!

推荐答案

丹尼斯(Dennis)的注释正确.这个想法是使用逻辑索引直接切出了第一步.因此,例如,如果您尝试提取矩阵中大于2的所有元素,则使用find可以做到这一点:

Dennis is correct in the comment. The idea is that using logical indexing directly cuts out a step. So if you're trying to extract all the elements in a matrix that are greater than 2 for example, using find you would do this:

A = [1 3 2 1 4 1]
A(find(A>2))

变成类似

A(find([0 1 0 0 1 0]))

然后

A([2, 5])

最后

[3, 4]

但是,如果您直接像这样直接使用逻辑索引:

However if you used logical indexing directly like this:

A(A>2)

你得到

A([0 0 1 0 0 1 0])

最后

[3,4]

因此,您获得完全相同的结果,并且跳过对find的调用,如您所见,在这些情况下,这完全是多余的.

So you get exactly the same result, and you skip a call to find which as you can see is completely extraneous in these cases.

然后只添加一些很酷的东西,除非您的Matlab很老,否则mlint(向您发出警告的位)实际上可以为您解决此问题.如果将鼠标悬停在用红色下划线标记的find上,则会得到以下信息:

Then just to add something pretty cool, unless your Matlab is pretty old, the mlint (the bit that gives you that warning) can actually fix this for you. If you hover over the find which is underlined in red you get this:

因此,这是该错误的基本版本,请参阅最后的一个小fix按钮.单击后将得到以下结果:

So this is a basic version of exactly that mistake, see at the end there is a little fix button. This is what you get after clicking it:

好的,在这种情况下,它是正常的索引编制,而不是逻辑索引,但重点仍然是,mlint可以为您解决这个问题,这真是太棒了!

OK so in this instance it's normal indexing instead of logical but the point remains, mlint can fix this for you which is pretty awesome!

这篇关于使用逻辑索引而不是FIND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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