Compare values within a matrix

1 次查看(过去 30 天)
Yewande Oni
Yewande Oni 2015-6-23
回答: Jan 2015-6-23
I am comparing two matrices of different dimensions (A and B). I want to find values in B that are within 1 number unit to A. Is there anyway I can do this? B is a smaller matrix than A.
Thanks

回答(2 个)

Ingrid
Ingrid 2015-6-23
if you mean to find all numbers that are equal to each other when disregarding the numbers behind the comma than you can use something like this
A_r = round(A);
B_r = round(B);
C = intersect(A_r,B_r);
  1 个评论
Yewande Oni
Yewande Oni 2015-6-23
编辑:Yewande Oni 2015-6-23
Hi Thanks but I want C to be a matrix of values that are within 1 unit of A, from B.
For example:
A = [31 16 8; 38 4 10]
B = [30 15 7; 34 5 14]
C would index that row 1 of A is a match to row 1 of B, because B is within +/- 1 of A
Is that possible?

请先登录,再进行评论。


Jan
Jan 2015-6-23
A = [31 16 8; 38 4 10];
B = [30 15 7; 34 5 14];
nA = size(A, 1);
match = false(1, nA)
for iA = 1:nA
D = abs(bsxfun(@minus, A(iA, :), B)) <= 1.0;
match(iA) = any(all(D, 2));
end

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by