【问题标题】:In matlab finding similar elemants in vectors of unequal length within a tolerance在matlab中在公差内找到长度不等的向量中的相似元素
【发布时间】:2013-03-04 21:10:43
【问题描述】:

我有两个长度不等的列向量,calib(75 个元素)和nu(1436 个元素)。我需要在nu 中找到与calib 中的元素匹配的元素,使其容差在0.001 以内,然后对结果向量执行操作。所有元素都是数字而不是字符串。

我认为 intersect 将是要走的路,但我看不到引入容差的方法。同样使用带有绝对差异语句的 for 循环似乎也不起作用。

任何帮助将不胜感激。

【问题讨论】:

标签: matlab vector compare


【解决方案1】:

您可以round 其中一个向量仅显示 0.001 的准确度,然后使用 ismember。 或者只使用来自 FEX 的 ismemberf - 浮点 ISMEMBER(即具有舍入公差)。

【讨论】:

    【解决方案2】:

    为此,我有另一种 Matlabic (=matrix) 方式。我不知道它的计算效率如何,但 Matlab 对矩阵应该很好。

    我假设size(nu) = [75,1]size(calib) = [1436,1]。第一步是制作两个 1436x75 的大矩阵(注意转置):

    calib_m = repmat(calib',size(nu))
    nu_m = repmat(nu,size(calib'))
    

    然后你可以再做一个矩阵1436x75就是上面的绝对差:

    diff_m = abs(calib_m - nu_m)
    

    现在找出每一列中最小的元素:

    min_m = min(diff_m)
    

    然后你可以引入你的阈值并做一个逻辑索引(应该很快):

    ok_calib_elements = calib(min_m < THRESHOLD)
    

    PS:我这里没有matlab,所以没有测试代码

    【讨论】:

    • 谢谢。我最终使用了一个变体。
    • 很高兴为您提供帮助。很高兴这是有效的。从 Matlab 切换到 Python 的工作,我有时会错过一些 Matlab 优雅的无循环编码。
    【解决方案3】:

    感谢您的帮助。矩阵数学把我带到了我需要去的地方。我不知道是否有更好的方法可以做到这一点,但这是我的代码:

    % set tolerance for finding equal values in the data
    find_tol = 1E-3; 
    
    % make matrices of the data and clibration vectors
    nu_m = repmat(nu',size(calib));
    calib_m = repmat(calib,size(nu'));
    
    % subtract the matrices
    diff_m = calib_m - nu_m;
    
    % find the minimum of each column within the tolerance and compute the
    % average shift
    find_min_nu = find(diff_m < find_tol & diff_m > -find_tol);
    min_nu = diff_m(find_min_nu);
    shift_nu = mean(min_nu);
    
    % output calibrated values
    calib_nu = nu + shift_nu;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      相关资源
      最近更新 更多