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 rows in matrix

Subject: combining rows in matrix

From: SM

Date: 11 May, 2010 08:33:04

Message: 1 of 4

Hi everybody,

I want to combine rows in a matrix in following way:
A=[a b c d
     e f g h
     i j k l];
if j=1
y=[a b c d];

if j=2
y=[a e b f c g d h];

if j=3
y=[a e i b f j c g k d h l];
....
is it possible to do this in matlab?

thanks for any advice.

Subject: combining rows in matrix

From: Sean

Date: 11 May, 2010 11:45:21

Message: 2 of 4

"SM " <khanmoradi@gmail.com> wrote in message <hsb4o0$8ra$1@fred.mathworks.com>...
> Hi everybody,
>
> I want to combine rows in a matrix in following way:
> A=[a b c d
> e f g h
> i j k l];
> if j=1
> y=[a b c d];
>
> if j=2
> y=[a e b f c g d h];
>
> if j=3
> y=[a e i b f j c g k d h l];
> ....
> is it possible to do this in matlab?

Sure, just about anything is possible in MATLAB!

%I'm going to use numbers instead of letters.
A = ceil(10*rand(3,4));
Ap = A';
j = 1; %can be 1, 2 or 3 for this 'A' matrix
y = Ap(1:j*4)';

Subject: combining rows in matrix

From: SM

Date: 11 May, 2010 11:59:04

Message: 3 of 4

> A = ceil(10*rand(3,4));
> Ap = A';
> j = 1; %can be 1, 2 or 3 for this 'A' matrix
> y = Ap(1:j*4)';
Actually, this only ordered the rows. What I want is something different.

Assume A = ceil(10*rand(3,4)) generates the following matrix:
A=[4 10 6 8
     4 7 7 3
     3 5 10 9]
in case j=2, I want the following outcome:
y= [4 4 10 7 6 7 8 3]
(while the provided code gives y=[4 10 6 8 4 7 7 3])...

is it possible to do this in matlab?

Subject: combining rows in matrix

From: Oleg Komarov

Date: 11 May, 2010 12:22:05

Message: 4 of 4

"SM " <khanmoradi@gmail.com> wrote in message <hsbgq8$ct7$1@fred.mathworks.com>...
> > A = ceil(10*rand(3,4));
> > Ap = A';
> > j = 1; %can be 1, 2 or 3 for this 'A' matrix
> > y = Ap(1:j*4)';
> Actually, this only ordered the rows. What I want is something different.
>
> Assume A = ceil(10*rand(3,4)) generates the following matrix:
> A=[4 10 6 8
> 4 7 7 3
> 3 5 10 9]
> in case j=2, I want the following outcome:
> y= [4 4 10 7 6 7 8 3]
> (while the provided code gives y=[4 10 6 8 4 7 7 3])...
>
> is it possible to do this in matlab?

A = [4 10 6 8
     4 7 7 3
     3 5 10 9];
j=2;

Out = reshape(A(1:j,:),1,[])
Out =
     4 4 10 7 6 7 8 3

Oleg

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