how to compare column values of a matrix with a column vector of different size?
조회 수: 2 (최근 30일)
이전 댓글 표시
giancarlo maldonado cardenas
2022년 6월 20일
댓글: giancarlo maldonado cardenas
2022년 6월 22일
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"]);
댓글 수: 0
채택된 답변
Adam Danz
2022년 6월 20일
편집: Adam Danz
2022년 6월 21일
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]
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]
댓글 수: 10
Adam Danz
2022년 6월 21일
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?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!