Logical comparison between two tables

1 visualización (últimos 30 días)
Khairul Nur
Khairul Nur el 6 de Ag. de 2021
Editada: Khairul Nur el 6 de Ag. de 2021
hi, i have two tables (value_a_per_cluster and calc_distance_min) which need to do logical comparison.
value_a_per_cluster =
0 1 1 2
1 0 2 1
1 2 0 1
2 1 1 0
calc_distance_min =
1.0000 1.4142 1.4142 1.7321
1.0000 0 1.4142 1.0000
1.7321 2.0000 1.4142 1.7321
1.7321 1.4142 1.4142 1.0000
each value in both tables need to do comparison as below:
if value_a_per_cluster <= calc_distance_min returns 2 else 3, thus the result should be:
2 2 2 3
2 2 3 2
2 2 2 2
3 2 2 2
Is it possible to do it without looping each rows and columns? Please suggest any function n code without doing the loops. TQIA

Respuesta aceptada

Dave B
Dave B el 6 de Ag. de 2021
Editada: Dave B el 6 de Ag. de 2021
As a quick answer, yes, you just use <= to compare the two matrices
You can take the 1s and 0s that result from doing the comparison and subtract them from 3 for an easy version that gives your answer:
value_a_per_cluster =[
0 1 1 2
1 0 2 1
1 2 0 1
2 1 1 0
];
calc_distance_min =[
1.0000 1.4142 1.4142 1.7321
1.0000 0 1.4142 1.0000
1.7321 2.0000 1.4142 1.7321
1.7321 1.4142 1.4142 1.0000
];
3-(value_a_per_cluster <= calc_distance_min)
ans = 4×4
2 2 2 3 2 2 3 2 2 2 2 2 3 2 2 2
A slightly more elegant final bit, that would extend more easily to other values, might be:
isIt = value_a_per_cluster <= calc_distance_min;
result = nan(size(isIt));
result(isIt) = 2;
result(~isIt) = 3;
result
result = 4×4
2 2 2 3 2 2 3 2 2 2 2 2 3 2 2 2
  1 comentario
Khairul Nur
Khairul Nur el 6 de Ag. de 2021
Editada: Khairul Nur el 6 de Ag. de 2021
less hassle without the loop and get what i want. TQVM for the time and code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by