最有效的方式查找数组中的值。

12个视图(30天)
我有一个数组(一种查找数组生成),我有8列和大约400行。在每一行的第一个四个值代表了边界(y1, x2, y2和x1)矩形区域的平面。最后四值是整数我想访问如果查询点区域内所描述的前四个值。矩形区域的完全覆盖飞机到一定限度,不重叠,但他们不定期/结构化。
如果我有1000万分(由x和y矢量和矢量),我想找到这些四个整数(在列8)对应于他们的地区,最快的方法是什么呢?改变我的方式查找数组代表了数据没有禁区,如果它有助于使过程更快。
我试过以下,这是缓慢的,似乎很低效和休息区域之间的边界上的点是:
QueryPoints = (X, Y);% 10000000 x 2向量为查询点。
n =长度(X);%确定数量的查询点
SearchIndex =所有([Y - X - Y X] > =排列([IndexList (: 1) -IndexList (: 2:3) IndexList (:, 4)], [3 2 1]), 2);
myIntegers = 0 (n, 4);
i = 1: n
myIntegers(我)= IndexList (SearchIndex(我1:),8);% 10000000 x 4向量与整数的集合对应的坐标
结束
谢谢。
4评论
约翰·Vormadal
约翰·Vormadal 2023年3月20日
是的,内存需求是我的一个问题,我没有提到。
这似乎是围绕这个问题的好方法。

登录置评。

接受的答案

约翰D 'Errico
约翰D 'Errico 2023年3月17日
编辑:约翰D 'Errico 2023年3月17日
疼我的头看到这订购:(y1, x2, y2和x1)。叹息。为什么?随你的船,我猜。
我不会使用一个循环。甚至没有关闭。即使是远程。我也不会是测试每一个矩形,因为您的代码。相反,我将寻找一种方法来解决这个使用现有的高效的代码为解决这个问题而设计的。
如果矩形区域完全砖平面区域,但不重叠,我会首先把每个矩形分成两个三角形。结果将是一个三角地区。所以我现在可能创建一个三角的三角形。不要使用德劳内。(我不知道你的问题是凸。)你已经知道矩形。这意味着你已经可以直接计算三角。
帮助三角测量
三角测量三角在二维或三维三角剖分的支持拓扑和几何查询2 d和3 d空间。金宝app例如,您可以查询三角形连接到一个顶点,三角形的邻居信息,外心等等。您可以创建一个三角直接使用现有三角矩阵中的数据格式。或者,您可以创建一个delaunayTriangulation,它提供了对三角函数的访问。TR =三角(三,X, Y)创建一个二维三角从三角连接矩阵三分(X, Y)。三是一个m-by-3矩阵m是三角形的数量。每一行定义一个三角形的三顶点id -点的行数(X, Y)。点坐标(X, Y)代表三角的点列向量。TR =三角(三,X, Y, Z)创建一个3 d三角从三角连接矩阵三分(X, Y, Z)。三是一个m-by-3 m-by-4矩阵表示m三角形或四面体,分别有3或4个顶点。每一行三定义一个三角形或四面体的顶点id -点的行数(X, Y, Z)。点坐标(X, Y, Z)代表三角的点列向量。TR =三角(三,P)创建一个三角的三角连接矩阵三分三页是一个m-by-3 m-by-4矩阵表示m三角形或四面体,分别有3或4个顶点。每一行三定义一个三角形或四面体的顶点id,点P的行数是一个mpts-by-ndim矩阵那些mpt是点的数量和ndim维度的数量,2 < = ndim < = 3。示例1:%加载一个2 d三角矩阵格式和使用三角%查询自由边界边。 load trimesh2d % This loads triangulation tri and vertex coordinates x, y trep = triangulation(tri, x,y); fe = freeBoundary(trep)'; triplot(trep); % Add the free edges in red hold on; plot(x(fe), y(fe), 'r','LineWidth',2); hold off; axis([-50 350 -50 350]); axis equal; Example 2: % Load a 3D tetrahedral triangulation in matrix format and use the % triangulation to compute the free boundary. load tetmesh % This loads triangulation tet and vertex coordinates X trep = triangulation(tet, X); [tri, Xb] = freeBoundary(trep); %Plot the surface mesh trisurf(tri, Xb(:,1), Xb(:,2), Xb(:,3), 'FaceColor', 'cyan', 'FaceAlpha', 0.8); Example 3: % Direct query of a 3D Delaunay triangulation created using % delaunayTriangulation. Compute the free boundary as in Example 2 Points = rand(50,3); dt = delaunayTriangulation(Points); [tri, Xb] = freeBoundary(dt); %Plot the surface mesh trisurf(tri, Xb(:,1), Xb(:,2), Xb(:,3), 'FaceColor', 'cyan','FaceAlpha', 0.8); triangulation methods: barycentricToCartesian - Converts the coordinates of a point from Barycentric to Cartesian cartesianToBarycentric - Converts the coordinates of a point from Cartesian to Barycentric circumcenter - Circumcenter of triangle or tetrahedron edgeAttachments - Triangles or tetrahedra attached to an edge edges - Triangulation edges faceNormal - Triangulation face normal featureEdges - Triangulation sharp edges freeBoundary - Triangulation facets referenced by only one triangle or tetrahedron incenter - Incenter of triangle or tetrahedron isConnected - Test if a pair of vertices is connected by an edge neighbors - Neighbors to a triangle or tetrahedron vertexAttachments - Triangles or tetrahedra attached to a vertex vertexNormal - Triangulation vertex normal size - Size of the triangulation ConnectivityList nearestNeighbor - Vertex closest to a specified point pointLocation - Triangle or tetrahedron containing a specified point triangulation properties: Points - The coordinates of the points in the triangulation ConnectivityList - The triangulation connectivity list See also delaunayTriangulation. Documentation for triangulation doc triangulation Other uses of triangulation polyshape/triangulation
方法三角测量
类三角测量的方法:barycentricToCartesian edgeAttachments featureEdges与pointLocation vertexAttachments cartesianToBarycentric边缘freeBoundary nearestNeighbor大小vertexNormal外心faceNormal内心邻居三角
正如您可以看到的,三角pointLocation方法。我可能会使用。
帮助三角/ pointLocation
包含指定点TI = pointLocation pointLocation三角形或四面体(T, QP)返回的索引查询点QP三角形/四面体封闭。矩阵QP包含查询点的坐标。QP是mpt mpts-by-ndim矩阵,是查询的数量分和2 < = ndim < = 3。透明国际是一个列向量三角形或四面体id对应T.ConnectivityList三角连接矩阵的行数。三角形/四面体封闭点QP (k,:)是TI (k)。返回NaN的点不是位于三角形或四面体T TI = pointLocation (T QX、QY)和TI = pointLocation (T QX、QY,求出)允许查询点时在替代列向量指定格式在2 d和3 d工作。(钛、BC) = pointLocation (T,…)的回报,此外,公元前质心坐标。公元前公元前mpts-by-ndim矩阵,每一行(我:)代表的质心坐标QP(我:)对封闭TI(我)。示例1:%点位置在2 d T =三角([1 2 4;1 4 3; 2 4 5],[0 0; 2 0; 0 1; 1 1; 2 1]); % Find the triangle that contains the following query point QP = [1, 0.5]; triplot(T,'-b*'), hold on plot(QP(:,1),QP(:,2),'ro') % The query point QP is located in a triangle with index TI = 1 TI = pointLocation(T, QP) Example 2: % Point Location in 3D plus barycentric coordinate evaluation % for a Delaunay triangulation x = rand(10,1); y = rand(10,1); z = rand(10,1); DT = delaunayTriangulation(x,y,z); % Find the tetrahedra that contain the following query points QP = [0.25 0.25 0.25; 0.5 0.5 0.5] [TI, BC] = pointLocation(DT, QP) See also triangulation, triangulation.nearestNeighbor, delaunayTriangulation. Documentation for triangulation/pointLocation doc triangulation/pointLocation
之前我做了太多,我可能看起来,看看其他的工具,例如,tsearch可能更快。
例如:
n = 21;
(X, Y) = ndgrid (linspace (0, 1, n));% 400矩形
XYgrid = [X (:), Y (:));
(ri, rj) = ndgrid (1: n - 1);
皮= sub2ind (n, n, ri (:), rj (:));
三=[[皮,皮+ 1,皮+ n];[皮+ n + 1,皮+ 1,皮+ n]];
triang =三角(三,X (:), Y (:));
rectid =(皮,皮);%每个三角形映射到它来自原来的矩形
%,生成1 e7点
xytest =兰德(1 e7, 2);
抽搐
triloc = pointLocation (triang xytest);
rectloc = rectid (triloc);
toc
运行时间是7.104490秒。
7秒的在线工具,(我的电脑总是更快运行时自己的MATLAB的副本。只有1.5秒在我自己的MATLAB的副本。)似乎是合理的定位1 e7点。事实上,当然,我可以做得更好了解的实际布局网格。但是你告诉我们,你的矩形不平凡地安排我在这个例子。和pointLocation不应该关心。
4评论
布鲁诺陈德良
布鲁诺陈德良 2023年3月19日
编辑:布鲁诺陈德良 2023年3月19日
德劳内并不没有凸如果我记得其中一个definntion circumscrbed圆在任何三角形不包含任何除了三角形的三个顶点。
它还可以派生的几何“双重”泰森多边形法。
我相信各种MATLAB tsearch使用德劳内的一些特殊属性,使搜索效率,如果不是它打破波动。
我记得在过去试图做插值,并提供自己的三角测量(例如spliiting一些来自其他地方的下结论四边形网格)和调用closestpoint我失败(或可能是过程需要太多的时间,我不记得我为什么把它的细节)。
也许我应该重温一遍。

登录置评。

答案(1)

骑自行车的人
骑自行车的人 2023年3月17日
编辑:骑自行车的人 2023年3月17日
一个简单的步骤,可以加速你的代码很多是preallocate内存输出数组。把
myIntegers = 0 (n, 4);
在你 循环。
你可以阅读关于为什么预先配置是非常重要的 在这里
1评论
约翰·Vormadal
约翰·Vormadal 2023年3月17日
编辑:约翰·Vormadal 2023年3月17日
对不起,我的代码片段是失踪的这条线。我目前预分配。我已经编出《华盛顿邮报》反映了这一点。

登录置评。

类别

找到更多的在德劳内三角帮助中心文件交换

下载188bet金宝搏


释放

R2022a

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!