unique values from from two column

조회 수: 8 (최근 30일)
Anu Sharma
Anu Sharma 2019년 8월 31일
댓글: Star Strider 2019년 9월 2일
I have a matrix
A=[1 2 3;
2 4 6;
5 3 2;
8 5 4;
6 7 8]
from this matrix, i want that value in first and second column will not get repeated. For eg. in 1st row 2 is in second column and in second row 2 is in 1st column. Please tell me how we can compare these two rows based on first two column and can get only that row whose value in the third column is greater.
Eg:
Output = [ 2 4 6;
8 5 4;
6 7 8]
Only two column (1, 2) will be compared to get the value from third column.
  댓글 수: 2
the cyclist
the cyclist 2019년 8월 31일
편집: the cyclist 2019년 8월 31일
Are you guaranteed that there will be at most two rows with equal values, as in your example? Or could it be like this ...
A=[1 2 3;
2 4 6;
5 2 7]
or like this ...
A=[1 2 3;
2 4 6;
5 2 7;
2 5 4]
Could you have a pattern that connects different rows, like this ...
A=[1 2 3;
2 5 6;
5 1 7]
(note that the 2 and the 5 commingle different rows) or like this ...
A=[5 2 3;
2 5 6]
Could you have equal values in the third column?
A = [1 2 6;
2 5 6];
Which row(s) should be kept?
Anu Sharma
Anu Sharma 2019년 8월 31일
yes cyclist, u r interpreting it correctly, plzz try helping me with some logic.

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

채택된 답변

Star Strider
Star Strider 2019년 8월 31일
The description of your problem is at best ambiguous.
This produces the result you posted:
Output = A(~any(A(:,[2 3]) == 2, 2),:)
produces:
Output =
2 4 6
8 5 4
6 7 8
It obviously tests on the presence of ‘2’ in the second and third columns, since ‘2’ appears to be important here. Note that in row 2 of your desired ‘Output’ matrix, column 3 is less than the other two elements. I see no pattern other than that expressed in my code.
  댓글 수: 8
Anu Sharma
Anu Sharma 2019년 9월 2일
@star Strider. Thanks for your previous help. But i was trying to implement the same logic for the data given below for getting the desired result and it is creating some problem please help me with this output.It is 23*3 matrix as input and 14*3 matrix as output with no repeated column value of 2 and 3
A=[29.78 5 8
24.97 8 11
22.98 4 12
21.05 12 13
24.78 1 16
25.53 26 29
21.43 2 32
29.94 11 33
29.57 15 35
28.43 17 36
11.49 23 37
13.69 37 38
26.97 28 39
16.25 25 40
27.36 36 41
4.24 18 42
19.39 39 44
29.93 16 45
25.83 30 46
26.09 40 47
27.58 24 48
28.61 41 49
29.41 48 50]
and i want
output =[22.98 4 12
25.53 26 29
21.43 2 32
29.94 11 33
29.57 15 35
28.43 17 36
13.69 37 38
26.97 28 39
4.24 18 42
29.93 16 45
25.83 30 46
26.09 40 47
28.61 41 49
29.41 48 50 ]
Star Strider
Star Strider 2019년 9월 2일
Try this:
[C,ia,ib] = intersect(A(:,2),A(:,3),'stable'); % Columns (2,3) Value Intersections
for k = 1:numel(ia)
Ac = {A(ia(k),:), A(ib(k),:)}; % Cell Array Of ‘Matching’ Rows
[~,Ix1] = max([A(ia(k),1), A(ib(k),1)]); % Index Of Maximum Of ‘Ac’
Output(k,:) = Ac{Ix1}; % Preliminary Output Matrix
end
Ix2 = setdiff((1:size(A,1)), [ia; ib]); % Other Rows
Output = [Output; A(Ix2,:)] % Complete Output Matrix
producing:
Output =
29.78 5 8
22.98 4 12
29.94 11 33
13.69 37 38
28.43 17 36
26.97 28 39
29.93 16 45
26.09 40 47
28.61 41 49
29.41 48 50
25.53 26 29
21.43 2 32
29.57 15 35
4.24 18 42
25.83 30 46
My initial intent was to avoid the (explicit) loop, however it turned out to be the best option. (This is a more general version of my earlier code.) When I checked the result, it appears to be correct, although it found one more row (row 1 in my result) than in your example.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by