Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
combining two vectors

Subject: combining two vectors

From: Steve Johnson

Date: 8 Sep, 2010 21:17:20

Message: 1 of 2

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?

Subject: combining two vectors

From: Sean

Date: 8 Sep, 2010 21:28:04

Message: 2 of 2

"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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us