MATLAB: Trying to find cells of a matrix from vector values

compare valuesmatrixvectors

I have 3 vectors
V1( 1; 3; 5;),
V2 (1; 3; 6;),
V3 (2; 3; 4; 5;) and i want to find in a matrix
A ( 1 5 2 4 3 1; 1 5 5 4 8 8; 5 2 4 6 3 10) at the respective row the 2 numbers closest to zero and return the number of the column. in my example it will be for the first row
V1 (1; 3;),
for the second V2 1; 3;),
and for the third V3 ( 2; 5;)
thank you

Best Answer

V1=[ 1; 3; 5]'
V2=[1; 3; 6]'
V3 =[2; 3; 4; 5]'
A =[ 1 5 3 4 3 1; 1 5 5 4 8 8; 5 2 4 6 3 10]
v={V1,V2,V3}
for k=1:size(A,1)
[ii,jj]=sort(A(k,v{k}));
out(k,1:2)=v{k}(jj(1:2));
end
disp(out)