Renaming values in an array
3 views (last 30 days)
Show older comments
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
Accepted Answer
Stephen23
on 4 Feb 2015
Edited: Stephen23
on 4 Feb 2015
>> [~,~,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
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');
More Answers (0)
See Also
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!