Clear Filters
Clear Filters

Get unique row indices in an array

1 view (last 30 days)
Chaitanya Sanghavi
Chaitanya Sanghavi on 27 Dec 2017
Eg. b is a matrix of size(17,2).
b(:,1)= [ 7;6;5;4;4;3;2;1;20;19;18;17;16;20;21;22;23]
b(:,2) = [30;29;28;27;8;9;10;11;8;12;13;14;15;27;26;25;24]
Now, I need the row indices from this array b which has matching entries in a vector a. The size of vector a is smaller than b. But if the indices are repeated, I want the smallest row entry.
Till this part, this is straight-forward.
The tricky part is here : lets say a is : [4 3 2 1]. The entries of a will either be in b(:,1) or b(:,2). there would surely not be any intersection. When I search for a in b(:,1) or (b(:,2)), i will get the smallest row indices [4; 6; 7; 8]. (These are the row indices.) But when I search for a = [20;21;22;23] in b(:,1), I want it to return the row indices as [8 13 14 15]. These row indices come from the fact that two indices are repeated before. so the total unique values in this array are only 15.
Note : The sequencing of the array b column-wise has to be preserved. I.e. the first column value in b, has some correspondence with the second column value in b. Same goes for all other entries. Thus using commands like " unique" would destroy this "sequencing".

Answers (0)

Community Treasure Hunt

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

Start Hunting!