Matlab:比较两个具有不同长度和不同值的向量吗? [英] Matlab: Comparing two vectors with different length and different values?

查看:397
本文介绍了Matlab:比较两个具有不同长度和不同值的向量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有两个向量A和B,它们的长度不同.Length(A) is not equal to Length(B),向量A中的值与向量B中的值不相同.我想将B的每个值与A的值进行比较(比较表示如果值B(i)与A(1:end)的值几乎相同,例如B(i)-Tolerance<A(i)<B(i)+Tolerance.

Lets say I have two vectors A and B with different lengths Length(A) is not equal to Length(B) and the Values in Vector A, are not the same as in Vector B. I want to compare each value of B with Values of A (Compare means if Value B(i) is almost the same value of A(1:end) for example B(i)-Tolerance<A(i)<B(i)+Tolerance.

由于数据量巨大,如何不使用for loop来做到这一点?

How Can I do this without using for loop since the data is huge?

我知道ismember(F),相交,复制,查找,但是这些功能都不能真正帮助我

I know ismember(F), intersect,repmat,find but non of those function can really help me

推荐答案

听起来您要执行的操作是拥有一个ismember函数用于实际值数据.

It sounds like what you are trying to do is have an ismember function for use on real valued data.

也就是说,检查向量B中的每个值B(i) B(i)是否在向量A

That is, check for each value B(i) in your vector B whether B(i) is within the tolerance threshold T of at least one value in your vector A

结果如下:

tf = false(1, length(b)); %//the result vector, true if that element of b is in a
t = 0.01; %// the tolerance threshold
for i = 1:length(b)
    %// is the absolute difference between the 
    %//element of a and b less that the threshold?
    matches = abs(a - b(i)) < t; 

    %// if b(i) matches any of the elements of a
    tf(i) = any(matches);
end

或者,简而言之:

t = 0.01;
tf = arrayfun(@(bi) any(abs(a - bi) < t), b);

关于避免for循环:虽然这可以从向量化中受益,但如果您的数据那么很大,您可能还需要考虑研究并行化.在那种情况下,像在我的第一个示例中那样具有for循环会很方便,因为您可以通过将for更改为parfor来轻松地进行并行处理的基本版本.

Regarding avoiding the for loop: while this might benefit from vectorization, you may also want to consider looking at parallelisation if your data is that huge. In that case having a for loop as in my first example can be handy since you can easily do a basic version of parallel processing by changing the for to parfor.

这篇关于Matlab:比较两个具有不同长度和不同值的向量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆