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:
doubts in matlab for combining two vectors

Subject: doubts in matlab for combining two vectors

From: Shobana

Date: 20 Dec, 2012 06:17:17

Message: 1 of 3

hai friends..

I want to merge two vectors like in the below specified manner
for example:
a= [1 , 2....n1]; b = [3 , 4.......n2]
then 'c' should be like this c = [1 3 2 4 .........n1,n2]
i tried like this c = [a,b] but this is not working
here the value of n1 and n2 will be known at the run time only.

Subject: doubts in matlab for combining two vectors

From: Roger Stafford

Date: 20 Dec, 2012 07:12:21

Message: 2 of 3

"Shobana " <manoharanshobana@ymail.com> wrote in message <kauahd$qj5$1@newscl01ah.mathworks.com>...
> I want to merge two vectors like in the below specified manner
> for example:
> a= [1 , 2....n1]; b = [3 , 4.......n2]
> then 'c' should be like this c = [1 3 2 4 .........n1,n2]
- - - - - - - - - - -
  Assuming that a and b are row vectors of possibly differing lengths, we append the excess of either at the right end:

  n = min(size(a,2),size(b,2));
  c = [reshape([a(1:n);b(1:n)],1,[]),a(n+1:end),b(n+1:end)];

Roger Stafford

Subject: doubts in matlab for combining two vectors

From: Nasser M. Abbasi

Date: 20 Dec, 2012 07:13:22

Message: 3 of 3

On 12/20/2012 12:17 AM, Shobana wrote:
> hai friends..
>
> I want to merge two vectors like in the below specified manner
> for example:
> a= [1 , 2....n1]; b = [3 , 4.......n2]
> then 'c' should be like this c = [1 3 2 4 .........n1,n2]
> i tried like this c = [a,b] but this is not working
> here the value of n1 and n2 will be known at the run time only.
>

may be:

----------------------
N = 4;
A = [1 2 3 4];
B = [3 4 5 6];
C = zeros(1,2*N);

C(1:2:2*N-1) = A;
C(2:2:2*N) = B
--------------------

C =
      1 3 2 4 3 5 4 6

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