フィルターのクリア

Sorting 2D matrix and retaining nodes in triangle groups

2 ビュー (過去 30 日間)
Fredrik P
Fredrik P 2019 年 4 月 30 日
回答済み: Fredrik P 2020 年 4 月 2 日
I am trying to optimize a script (below) that finds the bounding boxes to functional values of all lower left triangles in a 2D space. The code goes through all the triangles and then sorts the nodes in ascending order based on the functional values. This seems inefficient.
Is there some way that I could sort the functional values before the loops but still retain the nodes in the triangle groups? Or some other smart way to speed things up?
clear;
x = (1:600)';
y = (1:500);
z = 2 * x.^2 + y;
zGrid = linspace(min(z, [], 'all') - 1, max(z, [], 'all') + 1, 200);
for iX = 1:length(x) - 1
for iY = 1:length(y) - 1
% Node indices
xIndices = [iX, iX, iX + 1];
yIndices = [iY, iY + 1, iY];
% Node values
xTmp = x(xIndices);
yTmp = y(yIndices);
zTmp = z(sub2ind(size(z), xIndices, yIndices));
% Node sorted according to z
[zSorted, indicesSorted] = sort(zTmp);
xSorted = xTmp(indicesSorted);
ySorted = yTmp(indicesSorted);
% Get bounding box on zGrid
iMin = find(zGrid <= zSorted(1), 1, 'last');
iMax = find(zGrid(iMin:end) >= zSorted(end), 1, 'first') + (iMin - 1);
end
end

採用された回答

Fredrik P
Fredrik P 2020 年 4 月 2 日
See answer on Stack Overflow.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by