Assigning an index to each row based on column values in a matrix.
Mostra commenti meno recenti
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!
Risposta accettata
Più risposte (1)
Jan
il 16 Mar 2011
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 Commenti
Oleg Komarov
il 16 Mar 2011
The accumarray solution:
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];
C = [0;1;1;0;1;1;1;1;0;0;1;1;1;0;0;1;1;1];
[a,b,idx] = unique(A(:,1:2),'rows');
[B loc] = ismember(A,[a accumarray(idx,A(:,3),[],@min)],'rows');
B([false; diff(loc)==0],1) = false;
isequal(B,C)
Jan
il 16 Mar 2011
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
il 18 Mar 2011
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!