combining two vectors

  • Follow


So I've got a vector that needs to be sorted into a different output vector.
A= [1 2 3 4 5] is the input.
I must get an output such that out=[1 5 2 4 3]
So the output will always be in this pattern:
[min1, max1, min2, max2, min3, max3 ....etc.]

I know that to build this function, I must first arrange for the input vector to be sorted into two vectors, one descending and one ascending. Now the problem is assembling them into the new vector. Since the number of elements has to be the same as the original input vector, I'm not sure how to do this. Add in the fact that the ascending vector is in the even places, and the descending vector is in the odd places, and it gets too complicated for me. 
Can someone help me figure this out?
0
Reply Steve 9/8/2010 9:17:20 PM

"Steve Johnson" <xzibition86123@yahoo.com> wrote in message <i68uh0$1sn$1@fred.mathworks.com>...
> So I've got a vector that needs to be sorted into a different output vector.
> A= [1 2 3 4 5] is the input.
> I must get an output such that out=[1 5 2 4 3]
> So the output will always be in this pattern:
> [min1, max1, min2, max2, min3, max3 ....etc.]
> 
> I know that to build this function, I must first arrange for the input vector to be sorted into two vectors, one descending and one ascending. Now the problem is assembling them into the new vector. Since the number of elements has to be the same as the original input vector, I'm not sure how to do this. Add in the fact that the ascending vector is in the even places, and the descending vector is in the odd places, and it gets too complicated for me. 
> Can someone help me figure this out?

One way:
S = [sort(A,'ascend');sort(A,'descend')];
out = S(1:numel(A))
%Sean
0
Reply Sean 9/8/2010 9:28:04 PM


1 Replies
289 Views

(page loaded in 0.011 seconds)

Similiar Articles:













7/23/2012 10:36:45 AM


Reply: