Logical comparison between two tables

1 次查看(过去 30 天)
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

采纳的回答

Dave B
Dave B 2021-8-6
编辑:Dave B 2021-8-6
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 个评论
Khairul Nur
Khairul Nur 2021-8-6
编辑:Khairul Nur 2021-8-6
less hassle without the loop and get what i want. TQVM for the time and code.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by