You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻找find(ismember)高效替代方案:定位数组中行的位置索引

高效查找行在索引表中的位置优化方案

针对大数据量表下ismember(..., 'rows')速度慢的问题,以下是几种高效替代方案,均适配你的示例场景:

方案1:行转唯一标量键(整数元素通用)

将每行转换为唯一的64位整数键,利用标量查找的高效性替代行匹配:

% 自定义函数:将矩阵每行转换为唯一uint64键
function key = row_to_unique_key(mat)
    col_max = max(mat, [], 1);
    % 计算每列的权重,确保不同行不会生成重复键
    weights = cumprod([1, col_max(1:end-1)+1], 2);
    key = uint64(mat) * uint64(weights')';
end

% 生成键并查找
table_keys = row_to_unique_key(table_of_indices);
E_keys = row_to_unique_key(E);
pos = find(ismember(table_keys, E_keys));

验证示例:
输入你的测试数据,生成的table_keys为[13,25,34,14,129],E_keys为[14,13],最终pos返回[1,4],符合预期。

方案2:线性索引转换(正整数元素专属)

如果索引表元素均为正整数,可将两行视为二维数组坐标,转换为线性索引,速度最快:

max_val = max(table_of_indices(:));
% 将行转换为线性索引
table_lin_idx = sub2ind([max_val, max_val], table_of_indices(:,1), table_of_indices(:,2));
E_lin_idx = sub2ind([max_val, max_val], E(:,1), E(:,2));
% 查找匹配位置
pos = find(ismember(table_lin_idx, E_lin_idx));

注意:若存在负数元素,可先给所有元素加偏移量转为正整数(如mat = mat - min(mat(:)) + 1)。

方案3:排序后快速匹配(任意数值类型)

通过排序将行匹配转化为有序数组查找,适合非整数或大范围数值场景:

% 对索引表按行排序并保留原始索引
[sorted_table, orig_pos] = sortrows(table_of_indices);
% 对目标组按行排序,记录排序索引用于还原顺序
[sorted_E, E_sort_idx] = sortrows(E);
% 在有序表中匹配行
[~, match_locs] = ismember(sorted_E, sorted_table, 'rows');
% 过滤无效匹配并映射回原始位置
valid_locs = match_locs(match_locs ~= 0);
pos = orig_pos(valid_locs);
% (可选)还原为目标组E的原始顺序
[~, restore_idx] = sort(E_sort_idx);
pos = pos(restore_idx);

优势:sortrows和有序数组的ismember操作时间复杂度远低于原方法,大数据量下性能提升显著。

内容的提问来源于stack exchange,提问作者Spida

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 19:31:06