Renaming values in an array

3 views (last 30 days)
Jason
Jason on 4 Feb 2015
Commented: Stephen23 on 5 Feb 2015
I have an array obtained by vertcat as follows:
B =
01C1
01C1
01C1
01C1
01C2
01C2
01C2
02C1
02C1
02C2
03C1
03C2
03C2
04C1
04C1
04C1
04C2
04C2
04C2
04C2
05C1
05C1
05C1
05C2
05C2
06C1
06C1
06C2
I want to replace the text with numbers with the following pattern:
01C1=1
01C2=2
02C1=3
02C2=4
03C1=5
04C2=6
and so on.
so my list would begin like:
1
1
1
1
2
2
2
3
3
etc..
Thanks for any help.
  3 Comments
Jason
Jason on 4 Feb 2015
Oh yes, sorry you are right
Stephen23
Stephen23 on 5 Feb 2015
+1 for explaining the question clearly, with input and output examples.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Feb 2015
Edited: Stephen23 on 4 Feb 2015
You can use unique to achieve this:
>> [~,~,ic] = unique(B,'rows')
ic =
1
1
1
1
2
2
2
3
3
4
5
...
Note that this method works only if the rows of B occur in the order of the pattern that you specify.
  2 Comments
Guillaume
Guillaume on 4 Feb 2015
If you want to preserve the order of appearance in B in the return values of unique (i.e. a stable sort), you can with:
[~, ~, ic] = unique(B, 'rows', 'stable');

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!