MATLAB: Replacing values in a column vector.

matrix array

Hello.
I've a column vector 61312*1. Values ranges from 0 to 20. I need to replace the values (0 to 20) with the below mentioned values(0 to 7):
0=0, 1=1, 2,3=2, 4=3, 5,6,13=4, 7,8,9=5, 10,11,12,16=6, 14,15,17,18,19,20=7
Thanks in advance.

Best Answer

A=randi([0 20],20,1) % Example
a1={0 1 [2 3] 4 [5 6 13] [7 8 9] [10 11 12 16 ] [14 15 17 18 19 20]} % Numbers to be replaced
b1=[0 1 2 3 4 5 6 7] % Numbers that will replace your original array
idx=cellfun(@(x) ismember(A,x),a1,'un',0)
for k=1:numel(idx)
A(idx{k})=b1(k)
end