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

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

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

hello @Adam Danz, according to what I understand this line y = splitapply(@min,M_transmision(:,5),idx); calculate the minimum of the repeated numbers right?
How can I do in the event that there are no repeated numbers, that is, I need both ways, my numbers are random, sometimes they are repeated, sometimes they are not.
you can help?
The splitapply line returns the minimum value in colum 5 for rows that have the same value in column 2. If there are no repeat values in column 2, then it just returns the value in column 5.
For example,
min([4,3])
ans = 3
min(3)
ans = 3
If this doesn't address your follow-up questions, please provide an example where my solution fails.
in this case, for example, the detected vector and M_transmission, are always random, and sometimes there is an error in that line.
attached new vector detected.mat and M_transmision.mat
detected = detected.'; to make column vector.
detected = [61
49
40
34
30
26
25
18
8]
M_transmision = [1 61 -301 36 304
2 30 46 -50 68
3 26 -28 -189 191
4 49 -136 307 336
5 42 255 271 372
6 25 -167 -39 171
7 8 269 -306 407
8 34 -314 59 319
9 18 -49 107 118
10 40 -77 75 108]
In this case, an error also appears.
Error using splitapply (line 61)
Group numbers must be a vector of positive integers, and cannot be a sparse
vector.
Try the updated answer and let me know if the problem persists.
detected = [63
60
52
43
27
24
18
15
6
4]
M_ transmision = [ 1 6 -438 -218 489
2 63 34 4 34
3 4 120 -49 129
4 5 51 -57 77
5 43 -343 -257 429
6 18 -469 331 574
7 60 -503 16 503
8 24 -97 31 102
9 15 346 -148 376
10 52 -95 235 253
in this case a size error appears, in this line idx = findgroups(midx(ism));
cut my vector from 1x10 to 1x9, I don't know why!
my detected vector should not be cut, it should only be compared if the values that were detected exist in the transmission matrix and extract their distance. then I have to compare the distances in another process.
but there is missing a detected value.
> in this case a size error appears, in this line idx = findgroups(midx(ism))
I copied your inputs and added my solution and ran the results below. I don't see an error message.
> cut my vector from 1x10 to 1x9, I don't know why!
Hint: detected does not contain 5.
detected = [63
60
52
43
27
24
18
15
6
4];
M_transmision = [ 1 6 -438 -218 489
2 63 34 4 34
3 4 120 -49 129
4 5 51 -57 77
5 43 -343 -257 429
6 18 -469 331 574
7 60 -503 16 503
8 24 -97 31 102
9 15 346 -148 376
10 52 -95 235 253];
[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 = 9×2
63 34 60 503 52 253 43 429 27 102 24 574 18 376 15 489 6 129
Excuse me, I think I did not express myself well, at first the detected vector has 10 values, then in the line line idx = findgroups(midx(ism)). Cut my detected vector from 10 values to 9, would I have to continue having the 10 values, because I cannot eliminate any detected value from the detected vector, do I understand?
actually it would not be an error, it simply cannot eliminate a detected value, because the system already knows that there are 10 values, later I have to compare the distance of the X detected values, in this case the 10 values.
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?
thank you very much, I had to modify some things, to make it work in the following process.

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!