how to compare different matrix?

4 次查看(过去 30 天)
i want to compare two different matrix that has given below 'a' and 'b'
a=[ 'X' 'Y' 'Z'
30.4596 37.6811 93.0000
154.6678 172.8173 91.0000
44.7308 175.6077 94.0000
90.8282 191.0239 91.0000
149.5394 149.1930 70.0000];
b= 'Z'
[93.0000];
and now if 'a' has the value of 'b' in 3rd column of 'a', if this condition is satisfied than it should display corresponding remaining two value of that row, like this
c=[ 'X' 'Y' 'Z'
30.4596 37.6811 93.0000];

采纳的回答

Image Analyst
Image Analyst 2015-2-1
Try this vectorized approach:
a=[ ... %'X' 'Y' 'Z'
30.4596 37.6811 93.0000
154.6678 172.8173 91.0000
44.7308 175.6077 94.0000
90.8282 191.0239 91.0000
149.5394 149.1930 70.0000];
b= ... 'Z'
[93.0000];
tolerance = 0.00001;
% Subtract third columns
matches = abs(a(:,3) - b) < tolerance
% Extract matching rows.
c = a(matches, :)
  3 个评论
Matlab111
Matlab111 2015-2-1
编辑:Matlab111 2015-2-1
sir, one more question, how to delete that " Empty matrix".
NOTE: In this case if the value is not matched ('a' and 'b') than it's showing Empty matrix, so how to delete that.
Image Analyst
Image Analyst 2015-2-1
You can do clear('c') but then you won't have it later when you need to check on c. I'd just check it whenever you need to and not delete it:
if ~isempty(c)
% Some code
else
% Maybe exit from the function or something???
end

请先登录,再进行评论。

更多回答(1 个)

Geoff Hayes
Geoff Hayes 2015-2-1
Arul - if we assume that your b matrix has floating point values (as opposed to integers), then you may have to compare each value of b with each value in the third column of a and consider them identical if their absolute difference is less than some tolerance. For example,
tol = 0.00001;
for k=1:length(b)
valb = b(k);
for m=1:size(a,1)
vala = a(m);
if abs(valb- vala) < tol
% near identical!
% do something with mth row of a
% go to next element of b
break;
end
end
end
The above should provide a starting point from which you can elaborate on to solve your question.

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by