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

조회 수: 2(최근 30일)
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"]);

댓글을 달려면 로그인하십시오.

채택된 답변

편집: Adam Danz 2022년 6월 21일
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표시숨기기 이전 댓글 수: 9
giancarlo maldonado cardenas 2022년 6월 22일
thank you very much, I had to modify some things, to make it work in the following process.

댓글을 달려면 로그인하십시오.

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by