Assigning an index to each row based on column values in a matrix.

조회 수: 2 (최근 30일)
Manu
Manu 2011년 3월 16일
Hi, I have a matrix like this.
a= [ 1 2 8
1 2 2
1 3 1
1 2 5
2 4 3
3 5 4
4 6 2
4 7 1
4 6 5
4 6 3
5 7 3
6 8 3
6 9 1
6 9 1
6 9 5
7 6 4
7 8 5
9 8 2]
I want to assign an index to each row based on the following criterion - If the value in the first column and second column is same, I want to compare the third column value and assign an index 1 if it is the minimum among all rows with the same first and second column values and 0 otherwise. So for example, comparing first and second rows, they have the same corresponding first and second column values and second row has the minimum third column value among this. So the program should assign 0 for the first row and 1 for the second row.
Also, If all the three column values are same, I want to assign 1 to any one row and 0 to all other such rows.
So the ideal index vector I want for this is as follows
[0
1
1
0
1
1
1
1
0
0
1
1
1
0
0
1
1
1]
I wrote the following code to do this, but I am unable to get the required output.
minindex = ones(1,size(a,1))
for j=1:size(a,1)
for i = 1:size(a,1)
if ((a(j,1) == a(i,1)) && (a(j,2) == a(i,2)))
if (j~=i)
minindex(j) = (a(i,3) > a(j,3));
end
end
end
end
minindex'
Can any one help me on how to achieve this?
Thanks in advance!

채택된 답변

Matt Fig
Matt Fig 2011년 3월 16일
[As,I] = sortrows(a);
As = any([1 1;diff(As(:,1:2))],2);
I = As(I)
.
.
EDIT The above will not work always, but this should.
[As,I] = sortrows(a);
As = any([1 1;diff(As(:,1:2))],2);
Z = false(size(As,1),1);
Z(I(As)) = true
  댓글 수: 5
Manu
Manu 2011년 3월 18일
Hi Matt, I had an issue when I tried to run this on a = [1 2 947.5; 1 2 20; 1 2 25] and [ 1 2 4; 1 2 2; 1 2 2]. Here I got I = [ 0 0 1] instead of [0 1 0] as it should have been. Can you tell me what seems to be the issue?
Matt Fig
Matt Fig 2011년 3월 18일
Hmmm, you are correct. It looks like I needed another variable in there, but coincidentally it worked on your original data. I have changed the post to a more correct formulation. Thanks for catching that!

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

추가 답변 (1개)

Jan
Jan 2011년 3월 16일
Please try this (I cannot run it, because I have no access to Matalb currently):
a = [1 2 8; 1 2 2; 1 3 1; 1 2 5; 2 4 3; ...
3 5 4; 4 6 2; 4 7 1; 4 6 5; 4 6 3; ...
5 7 3; 6 8 3; 6 9 1; 6 9 1; 6 9 5; ...
7 6 4; 7 8 5; 9 8 2];
[ua, ia, ja] = unique(a(:, 1:2), 'rows');
n = max(ja); % Number of different rows
minindex = zeros(size(a, 1), 1);
for i = 1:n
index = find(ja == i);
[dummy, q] = min(a(index, 3));
minindex(index(q)) = 1;
end
I assume there is a shorter way using ACCUMARRAY, but I cannot figure it out.
  댓글 수: 3
Jan
Jan 2011년 3월 16일
The ACCUMARRAY is shorter, but not really nicer. In addition UNIQUE and ISMEMBER perform a time-consuming sorting. Therefore I'd prefer the FOR method - but Matt's easier SORTROWS approach rules.
Manu
Manu 2011년 3월 18일
Hi Jan,
I had an issue when I tried to run Matt's code on a = [1 2 947.5; 1 2 20; 1 2 25] and [ 1 2 4; 1 2 2; 1 2 2]. Here I got I = [ 0 0 1] instead of [0 1 0] as it should have been. It looks like since sort rows doesn't sort from top down, the 'As' in the second step is [2 3 1] instead of [2 1 3]. Is there any way to correct this?
Thanks!

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

카테고리

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