MATLAB快速找到矩阵中的向量 [英] MATLAB quick find of a vector in matrix

查看:952
本文介绍了MATLAB快速找到矩阵中的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码可以按以下方式工作. 有一个大小为n x 2的矩阵.每个元素都是1到某个最大值之间的整数,例如m.

I have a piece of code that works in the following way. There is a matrix of size n x 2. Each element is an integer between 1 and some maximum, say m.

我要搜索此矩阵中的行,也就是说,给定[v1, v2],请输出此索引.

I want to search for rows in this matrix, that is, given [v1, v2], output the index of this.

现在,我正在使用:

k = find(ismember(edges, [v1, v2], 'rows'));

但是,这是我代码中的瓶颈,因为这是线性时间.

However, this is the bottleneck in my code because this is in linear time.

我想实现一些hashmap类型结构以进行快速查找.这样做的简单方法是什么?

I would like to implement some hashmap type structure for fast lookup. What would be an easy way to do this?

推荐答案

R2016b及以后版本:

R2016b and beyond:

find(all(edges == [v1 v2], 2))

之前:

find(all(bsxfun(@eq, edges, [v1 v2]), 2))

这篇关于MATLAB快速找到矩阵中的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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