In Matlab, the [C, ia, ic] = unique(A)
function returns an sorted array C
in which the duplicates in A
are removed, and the ia
and ic
arrays contains the indices such that C = A(ia) and A = C(ic)
. For example:
A = [1, 2, 3, -1, -2, 0, 0, 0, 4];
[C, ia, ic] = unique(A);
C = [-2, -1, 0, 1, 2, 3, 4];
% The indices are incremented by 1 to accomodate the C++ convection.
ia = [4, 3, 7, 0, 1, 2, 8]; % size(ia) = size(C)
ic = [3, 4, 5, 1, 0, 2, 2, 2, 6]; % size(ic) = size(A)
Here is my implementation:
std::vector<int> A = {1, 2, 3, -1, -2, 0, 0, 0, 4};
std::set<int> C(A.begin(), A.end());
std::vector<int> ic;
ic.reserve(A.size());
std::transform(A.begin(), A.end(), std::back_inserter(ic),
[&](int x) { return (std::distance(C.begin(), C.find(x))); });
I now can correctly get the array C and ic, but I don't know how to get the array ia.
Can someone help me with this?
Aucun commentaire:
Enregistrer un commentaire