【问题标题】:compare two vectors with different size in matlab在matlab中比较两个不同大小的向量
【发布时间】:2014-06-04 02:02:48
【问题描述】:

我有两个向量 ab 具有不同的大小 size(A) = n & size(b) = m 和 (n>m),我想得到 (n-m) 值比较后两个向量中的元素最接近其他元素

a = [17    28    41    56    69    84   102   105   123   138   149   160   173   184]              

b = [17    28    42    56    69    83   123   138   149   159   173   185]

我需要的是

result = [102 105] 

用matlab

谁能帮帮我?

【问题讨论】:

  • 对不起,我不明白。您想要在一个中但不在另一个中的元素具有相等的三分法吗?
  • 根据此处要求的内容,这可能是重复的:stackoverflow.com/q/17612144/1133144
  • 不,我正在尝试应用“曲率比例空间”所以我有具有最大曲率点的边界对象轮廓的 CSS 图像,我想提取这些点的坐标。更清楚的可以看这篇论文pdf.aminer.org/000/312/062/…
  • 啊,这听起来像Dynamic Time Warping,然后是识别最差拟合的点。它也让我想起了localised contour sequences,这可能是一些次要的兴趣。

标签: matlab


【解决方案1】:
dist = abs(bsxfun(@minus, a(:).', b(:))); %'// distance for all combinations
[~, ind] = sort(min(dist),'descend'); %// compute minimum distance for each
%// element of a, and sort
result = a(ind(1:(numel(a)-numel(b)))); %// pick n-m elements of a with largest
%// minimum distance

【讨论】:

  • Gooooooooooooooooooooood,非常感谢 Luis Mendo,现在它可以和我一起使用了
【解决方案2】:

乍一看,如果您只处理整数值,您似乎可以不用setdiff,但这实际上有点复杂。

如果我正确理解了场景,您希望在数组a 中找到与数组b 中的任何元素最不相似的D(其中D = n-m)值。

Luis Mendo 给出的解决方案可以实现这一点,但是它涉及到 MxN 的内存复杂性,这对于更大的数组来说可能是令人望而却步的。

另一种方法如下:

[~, mi] = sort(arrayfun(@(ai) min(abs(b - ai)), a), 'descend');
c = a(mi(1:length(a) - length(b)));

或者,以更扩展的格式:

aDiff = nan(1,length(a));
for ai = 1:length(a) %//for each element in A, find...
    aDiff(ai) = min(abs(b - a(ai))); %//the difference from it's closest match in b
end

[~, mi] = sort(aDiff, 'descend'); %//find the elements with the largest difference
d = length(a) - length(b); %//determine how many to return
c = a(mi(1:d)); %//take the D elements with the biggest difference from A

这些方法的好处是,它将内存需求保持在2*length(a) 左右,并且可以在必要时在多个内核上并行化。

【讨论】:

  • 我尝试使用 setdiff 和 intersect 获得结果但没有成功,谢谢您的帮助
  • 啊,当然,你有一个小的匹配阈值需要考虑。
  • 是的,它也可以,很抱歉我不知道 matlab 中的所有这些命令(arrayfun,bsxfun,...),因为我是新用户。非常感谢艾伦
  • @user3496585 是的,对于新用户来说,它们似乎很深奥。它们基本上是for 循环的简写。例如:bob = arrayfun(@(x) x - 2, a) 将转换为:“从数组 a 中的每个元素中减去 2 并将结果放入数组 bob”。 bsxfun 类似,但您需要提供两个数组并且必须提供一个二进制函数(一个需要两个输入的函数)。
猜你喜欢
  • 1970-01-01
  • 2012-10-15
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多