MATLAB Answers

how to compare different matrix?

29 views (last 30 days)
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];

Accepted Answer

Image Analyst
Image Analyst on 1 Feb 2015
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 Comments
Image Analyst
Image Analyst on 1 Feb 2015
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

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 1 Feb 2015
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.

Community Treasure Hunt

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

Start Hunting!