how to compare different matrix?

조회 수: 10 (최근 30일)
Matlab111
Matlab111 2015년 2월 1일
댓글: Image Analyst 2015년 2월 1일
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에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by