Let's say I have a vector A = [-3 -2 -1 1 2].
I want to pick out the positive element closest to zero and the negative element closest to zero while retaining their index value in the vector A.
So I want another vector with [-1, 4; 1,5] (i.e. [-ve to 0, index ; +ve to 0, index]).
I've tried creating separate arrays with the positive and negative components and selecting the min/max of these, but this loses the index in A. E.g.
Ap = A(A>0)
An = A(A<0)
[ap,idxp]=sort(Ap)
[an,idxn]=sort(An)
I'm sure there must be a simple way to do this. Any help greatly appreciated.