Got Questions? Get Answers.
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:
beginner_question: expand a matrix

Subject: beginner_question: expand a matrix

From: joey hart

Date: 30 Sep, 2010 08:30:49

Message: 1 of 2

Hello everyone,

If I have a matrix :

A = [ 1 2
         3 4
         5 6]

How can I expand to the form:

--> [ 1 2 3 4
                   1 2 5 6
                   3 4 1 2
                   3 4 5 6
                   5 6 1 2
                   5 6 3 4]

regard
joey

Subject: beginner_question: expand a matrix

From: Jos (10584)

Date: 30 Sep, 2010 09:17:08

Message: 2 of 2

"joey hart" <joeyz0123@gmail.com> wrote in message <i81hrp$1vf$1@fred.mathworks.com>...
> Hello everyone,
>
> If I have a matrix :
>
> A = [ 1 2
> 3 4
> 5 6]
>
> How can I expand to the form:
>
> --> [ 1 2 3 4
> 1 2 5 6
> 3 4 1 2
> 3 4 5 6
> 5 6 1 2
> 5 6 3 4]
>
> regard
> joey


help NCHOOSEK en learn about the power of indexing
A = [ 1 2
         3 4
         5 6] ; % data
A = 10*A % to avoid confusion ...

k = 1:size(A,1)
ix = nchoosek(k,2) % half of the possibilities
% see NCHOOSE2 on the FEX for a faster implementation
% http://www.mathworks.com/matlabcentral/fileexchange/20144
ix = [ix ; k(end)-ix+1] % add the other half
ix = sortrows(ix) ; % sorted as desired
ix1 = ix(:,1)
ix2 = ix(:,2)
c12 = A(ix1,:) % the first 2 columns
c34 = A(ix2,:)
B = [c12 c34]

hth
Jos

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