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:
question in vectors

Subject: question in vectors

From: Konst

Date: 24 Jan, 2011 15:36:04

Message: 1 of 7

I have to row vectors A=[a1 a2 a3.....aN] and B=[b1 b2 b3....bN]
How can I create another vector that contains their values in this order:
table=[a1 b1 a2 b2...aN bN]????
Maybe it's easy but I'm stuck so please help!
Thanks for your time..!

Subject: question in vectors

From: Nasser M. Abbasi

Date: 24 Jan, 2011 15:45:35

Message: 2 of 7

On 1/24/2011 7:36 AM, Konst wrote:
> I have to row vectors A=[a1 a2 a3.....aN] and B=[b1 b2 b3....bN]
> How can I create another vector that contains their values in this order:
> table=[a1 b1 a2 b2...aN bN]????
> Maybe it's easy but I'm stuck so please help!
> Thanks for your time..!

may be

A=[1 2 3];
B=[4 5 6];
C=zeros(length(A)+length(B),1);
C(1:2:end-1)=A;
C(2:2:end)=B;
C

C =
      1
      4
      2
      5
      3
      6

--Nasser

Subject: question in vectors

From: James Tursa

Date: 24 Jan, 2011 15:51:04

Message: 3 of 7

"Konst " <konstance1@hotmail.com> wrote in message <ihk694$bap$1@fred.mathworks.com>...
> I have to row vectors A=[a1 a2 a3.....aN] and B=[b1 b2 b3....bN]
> How can I create another vector that contains their values in this order:
> table=[a1 b1 a2 b2...aN bN]????
> Maybe it's easy but I'm stuck so please help!
> Thanks for your time..!

C = reshape([A;B],1,[]);

James Tursa

Subject: question in vectors

From: someone

Date: 24 Jan, 2011 15:51:04

Message: 4 of 7

"Konst " <konstance1@hotmail.com> wrote in message <ihk694$bap$1@fred.mathworks.com>...
> I have to row vectors A=[a1 a2 a3.....aN] and B=[b1 b2 b3....bN]
> How can I create another vector that contains their values in this order:
> table=[a1 b1 a2 b2...aN bN]????
> Maybe it's easy but I'm stuck so please help!
> Thanks for your time..!

% Here is one way:

A = [1 2 3];
B = [4 5 6];
C = [A;B];
D = C(:).'

D =

     1 4 2 5 3 6

Subject: question in vectors

From: user

Date: 24 Jan, 2011 15:51:04

Message: 5 of 7

"Konst " <konstance1@hotmail.com> wrote in message <ihk694$bap$1@fred.mathworks.com>...
> I have to row vectors A=[a1 a2 a3.....aN] and B=[b1 b2 b3....bN]
> How can I create another vector that contains their values in this order:
> table=[a1 b1 a2 b2...aN bN]????
> Maybe it's easy but I'm stuck so please help!
> Thanks for your time..!
Hi
you can use for loop to create a new vector. ( if dim(B)=dim(A) )
j=1;
for i=1: aN
c(j) = A(j);
c(j+1)=B(j);
j=j+2;
end
But if your vector sizes are big, then obviously your computational time will increase using for loop.
May be there will be some alternative using matlab functions.

HTH,
Saif

Subject: question in vectors

From: Think blue, count two.

Date: 24 Jan, 2011 16:04:22

Message: 6 of 7

On 24/01/11 9:36 AM, Konst wrote:
> I have to row vectors A=[a1 a2 a3.....aN] and B=[b1 b2 b3....bN]
> How can I create another vector that contains their values in this order:
> table=[a1 b1 a2 b2...aN bN]????
> Maybe it's easy but I'm stuck so please help!
> Thanks for your time..!

reshape([A; B], 1, [])

Subject: question in vectors

From: Konst

Date: 24 Jan, 2011 16:16:04

Message: 7 of 7

Thank you all very much for your quick replies!

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