MATLAB: Prelocation of array entries

arrays

Let's say I have the following array: A = [2 4 6 8] and I want array B to be populated according to the following pattern: (entry from A)-1, entry from A, (entry from A)+1 so in this case B = [1 2 3 3 4 5 5 6 7 7 8 9]
How do I do this?
Thanks

Best Answer

A = [2 4 6 8]
B=cell2mat(arrayfun(@(x) [x-1 x x+1],A,'un',0))
%or
B=[A-1; A ;A+1]
B=B(:)'