how to compare column values of a matrix with a column vector of different size?

2 views (last 30 days)
Hello everyone, how can I compare a 10x5 matrix with a row vector, let me explain it better.
I have the vector
detected = [56
40
33
31
28
13
10
1]
and the matrix
M_transmision = 1 40 -84 -638 644
2 1 138 -276 308
3 31 -74 -157 173
4 28 -274 -511 580
5 13 53 -35 64
6 13 124 -367 388
7 56 30 -290 292
8 33 20 -263 263
9 28 -504 103 515
10 10 -118 -226 255
I need to compare the unique values of the detected vector with column 2 of the M_transmission matrix, and extract column 5 of the matrix from each unique value found.
for example the result would be the following.
result = [56 292
40 644
33 263
31 173
28 515
13 64
10 255
1 308]
note: If the numbers are repeated, as shown in this example, the number 13 and 28 are repeated, you have to extract the smallest number from column 5 of the matrix.
for instance
number 13 has:
13 64
13,388
and the number 28 has:
28,580
28,515
has to extract the smallest value, that is:
13 64 and 28 515
any help i will appreciate it
I was trying to do it like this, but I can't
detected(:,end+1:5)=missing;
detected = array2table(detected);
M_transmision = array2table(M_transmision);
result2 =innerjoin(M_transmision,detected,'LeftKeys',["M_transmision2"],'RightKeys',["detected1"]);

Accepted Answer

Adam Danz
Adam Danz on 20 Jun 2022
Edited: Adam Danz on 21 Jun 2022
Load data
detected = [56
40
33
31
28
13
10
1];
M_transmision = [1 40 -84 -638 644
2 1 138 -276 308
3 31 -74 -157 173
4 28 -274 -511 580
5 13 53 -35 64
6 13 124 -367 388
7 56 30 -290 292
8 33 20 -263 263
9 28 -504 103 515
10 10 -118 -226 255]; %
Return minimum of column 5 within groups defined by detected and column 2.
[~, idx] = ismember(M_transmision(:,2),detected);
y = splitapply(@min,M_transmision(:,5),idx);
result = [detected(unique(idx)), y]
result = 8×2
56 292 40 644 33 263 31 173 28 515 13 64 10 255 1 308
However, if there is a value from detected that is not in column 2 of M_transision, then this will cause an error. I assume detected = unique(M_transmision(:,2)) in which case, this won't be a problem.
Update
This version deals with mismatches between the two vectors better.
[ism, midx] = ismember(M_transmision(:,2),detected);
idx = findgroups(midx(ism));
y = splitapply(@min,M_transmision(ism,5),idx);
result = [detected(unique(idx)), y]
result = 8×2
56 292 40 644 33 263 31 173 28 515 13 64 10 255 1 308
  10 Comments
Adam Danz
Adam Danz on 21 Jun 2022
Column 2 of the M_transmision matrix contains a 5 but the detected vector does not contain a 5. Where should [5,77] go in the results?

Sign in to comment.

More Answers (0)

Categories

Find more on Data Preprocessing in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!