在MATLAB中模拟级联索引的一些好方法是什么? [英] What are some good ways to emulate cascading indexing in MATLAB?

查看:96
本文介绍了在MATLAB中模拟级联索引的一些好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我想做这样的事情:

E.g. I would like to do things such as:

A=4:20;
find(A>5)(2) % want to access the 2nd element of the index array returned by find

推荐答案

是的,此相当 经常不同的上下文中 >,并且单行答案为subsref.对于您的情况,就是这样:

Yes, this comes up fairly frequently in different contexts, and the one-line answer is subsref. For your case, it is this:

subsref(find(A>5),struct('type','()','subs',{{2}}))

更清洁的解决方案使用未记录的builtin:

A much cleaner solution uses an undocumented builtin:

builtin('_paren',find(A>5),2)

作为丑陋语法或未记录功能的替代方法,您可以定义一个类似于以下内容的小型函数,

As an alternative to ugly syntax or undocumented functionality, you could define a small function like the following,

function outarray = nextind(inarray,inds)
outarray = inarray(inds);

或内联函数:

nextind = @(v,ii) v(ii);

并像nextind(find(A>5),2)一样调用它.这比subsref干净,如果要执行线性索引(不是下标),则很好.

And call it like nextind(find(A>5),2). This is cleaner than subsref and good if you are doing linear indexing (not subscripts).

这篇关于在MATLAB中模拟级联索引的一些好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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