Question: Given a vector `A` of group numbers (such as the one returned by `findgroups`), how to return a vector `B` of the same length containing indices of elements within groups of `A`?

For example, if `A = [1 1 1 2 2 2 1 1 2 2]` then `B = [1 2 3 1 2 3 4 5 4 5]`.

**Add 1**

My own solution to this is

```
s = splitapply(@(x) {x, [1:numel(x)]'}, [1:numel(A)]', A(:))
B(vertcat(s{:,1})) = vertcat(s{:,2})

```

but it seems somewhat convoluted.

Answer: 
A solution using `sort` and `accumarray`:

```
[s is]=sort(A);
idx = accumarray(s(:),1,[],@(x){1:numel(x)});
B(is)=[idx{:}];

```

Another solution using the image processing toolbox:

```
p=regionprops(A,'PixelIdxList');
B = zeros(size(A));
for k = 1: numel(p)
    B(p(k).PixelIdxList) = 1:numel(p(k).PixelIdxList);
end

```
A solution can be using a loop to replace the values in `A`:

```
c = unique(A);
B= A;
for (idx = c)
    f = find(A == idx);
    B(f) = 1:length(f);
 end

```