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:
reorder matrix elements in columns

Subject: reorder matrix elements in columns

From: Wendy

Date: 12 Dec, 2010 21:21:05

Message: 1 of 2

Hi all,

I need some help to optimize a code. If I have an original matrix (test_input), and the order of the elements in each column (test_order). For example,

test_input =

    81 10 16
    91 28 97
    13 55 96
    91 96 49
    63 96 80

test_order =

     5 2 5
     2 1 4
     1 3 1
     3 4 3
     4 5 2

I want to rearrange the elements in each column of 'test_input' according to the order in the 'test_order', so the output that I am looking for is

test_output =
63 28 80
91 10 49
81 55 16
13 96 96
91 96 97

I tried 'test_input(test_order)', but it did not work. Is there a column function that I can use here.

Thank you very much.
Wendy

Subject: reorder matrix elements in columns

From: Oleg Komarov

Date: 12 Dec, 2010 22:37:04

Message: 2 of 2

test_input = [81 10 16
                 91 28 97
                 13 55 96
                 91 96 49
                 63 96 80];
test_order = [5 2 5
                 2 1 4
                 1 3 1
                 3 4 3
                 4 5 2];

% Increment test_order by [0,5,10]
idx = bsxfun(@plus, test_order,0:size(test_order,1):numel(test_order)-1);

test_input(idx)

Oleg

Tags for 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