how to compare different matrix?

4 visualizaciones (últimos 30 días)
Matlab111
Matlab111 el 1 de Feb. de 2015
Comentada: Image Analyst el 1 de Feb. de 2015
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];

Respuesta aceptada

Image Analyst
Image Analyst el 1 de Feb. de 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 comentarios
Matlab111
Matlab111 el 1 de Feb. de 2015
Editada: Matlab111 el 1 de Feb. de 2015
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 el 1 de Feb. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Geoff Hayes
Geoff Hayes el 1 de Feb. de 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.

Categorías

Más información sobre Sparse Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by