1

I want to map the values of an array to a new array. The transformation should map arbitrary values in the original array to the value of the order in which that value was first observed.

For example, this mapping would map the first element of the array, x, to the value 1. All additional instances of x would be mapped to 1 as well. The next value in the array, y, would be mapped to the value 2. All additional instances of y would be mapped to 2 as well, and so on.

example:

array     = [5 5 3 8 5 2 1 7 6 8 8 2 7 7 7 4];
new_array = [1 1 2 3 1 4 5 6 7 3 3 2 6 6 6 8];

another example:

array     = [2 7 3 3 4 4 4 7 7 1 1 5 8 6 3 8 4 4 3 3 6 6];
new_array = [1 2 3 3 4 4 4 2 2 5 5 6 7 8 3 7 4 4 3 3 8 8];

1 Answer 1

3

You should use unique.

[C, ia, ic] = unique(A,'stable');

then, ic' is your answer.

A = [5 5 3 8 5 2 1 7 6 8 8 2 7 7 7 4];
[C, ia, ic] = unique(A,'stable');
ic'
ans =
 1   1   2   3   1   4   5   6   7   3   3   4   6   6   6   8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.