MATLAB: Adding data to two matrices for indexing

indexingmatrix

I have 2 matrices Z and Y, both are a 10×1300 matrix and consists of random numbers, how do i add data to the left and right of each element and then use indexing to find Y for a certain value of Z
For example,
Z [10 1300]
Y [10 1300]
One element in Z:
Z(1,1)=10
z=Z(1,1)
Add data to left and right side,
z-3,z-2,z-1,z,z+1,z+2,z+3
Final result: 7,8,9,10,11,12,13
Similar for Y but with different data additions
One element in Y:
Y(1,1)=20
y=Y(1,1)
Add data to left and right side,
y+3,y+2,y+1,y,y+1,y+2,y+3
Final result: 23,22,21,20,21,22,23
Use indexing to find Z=7 then find the respective element from Y would give Y= 23,
Y(find(Z==7))=23
Z=8,Y=22
Z=9,Y=21
Z=10,Y=20
Z=11,Y=21
Z=12,Y=22
Z=13,Z=23

Best Answer

>> Z = [1,9;6,1];
>> cell2mat(arrayfun(@(m)m-3:+1:m+3,Z,'uni',0))
ans =
-2 -1 0 1 2 3 4 6 7 8 9 10 11 12
3 4 5 6 7 8 9 -2 -1 0 1 2 3 4
>> Y = [9,1;4,9];
>> cell2mat(arrayfun(@(m)[m+3:-1:m,m+1:m+3],Y,'uni',0))
ans =
12 11 10 9 10 11 12 4 3 2 1 2 3 4
7 6 5 4 5 6 7 12 11 10 9 10 11 12
>>