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

New to MATLAB?

How do I reverse the order of a vector?

Asked by Umer

Umer (view profile)

on 9 Feb 2011

Generate a 4x4 matrix P, whose first column is an array of 0, 2, 4 and 6; second column is an array of 1, 3, 5, and 7; third is the second column in reverse order and fourth column is the first column in reverse order. I want to use Reverse function or something like that..

0 Comments

Umer

Umer (view profile)

Products

8 Answers

Answer by Andreas Goser

Andreas Goser (view profile)

on 9 Feb 2011

I think you are looking for commands like flipud and fliplr, right?

0 Comments

Andreas Goser

Andreas Goser (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 9 Feb 2011

It would be an inefficient idea to use a function to create a 4x4 matrix with fixed values!

P = [0, 1, 7, 6; ...
     2, 3, 5, 4; ...
     4, 5, 3, 2; ...
     6, 7, 1, 0];

Now this homework is solved. Kind regards to your teacher.

4 Comments

Jan Simon

Jan Simon (view profile)

on 9 Feb 2011

I think this is more prone to errors and confuses beginners.

Walter Roberson

Walter Roberson (view profile)

on 10 Feb 2011

I think of it as a way of differentiating between those who learn and those who copy without understanding.

Jim Dowd

Jim Dowd (view profile)

on 10 Feb 2011

Funny stuff Jan Simon!

Jan Simon

Jan Simon (view profile)

Answer by Matt Fig

Matt Fig (view profile)

on 10 Feb 2011

Walter, I am shocked that you didn't include the obvious nested FOR loop.

cnt = 0;
for ii = 1:2,
    for jj = 1:4
        A(4-jj+1,4-ii+1) = cnt;
        A(jj,ii) = cnt;
        cnt = cnt + 2;
    end
    cnt = 1;
end

1 Comment

Walter Roberson

Walter Roberson (view profile)

on 10 Feb 2011

I was working on fitting it to a 100'th order polynomial, but discovered that any order beyond 23 gives absolutely completely wrong answers!

Matt Fig

Matt Fig (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 9 Feb 2011

t = (0:2:6).'; P = [t, t+1, 7-t, 6-t];

1 Comment

Andrew Newell

Andrew Newell (view profile)

on 10 Feb 2011

That told him!

Walter Roberson

Walter Roberson (view profile)

Answer by Matt Fig

Matt Fig (view profile)

on 10 Feb 2011

Or, a one liner:

reshape(permute(reshape([0:7 7:-1:0],2,4,2),[2,1,3]),4,4)

0 Comments

Matt Fig

Matt Fig (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 9 Feb 2011
t = (0:2:6).';
trev = t(length(t):-1:1);
P = [t, t+1, trev+1, trev];

0 Comments

Walter Roberson

Walter Roberson (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 9 Feb 2011
t = (0:2:6).'
trev = zeros(size(t));
trev(end:-1:1) = t;
P = [t, t+1, trev+1, trev];

0 Comments

Walter Roberson

Walter Roberson (view profile)

Answer by Walter Roberson

Walter Roberson (view profile)

on 9 Feb 2011
for K = 1 : 4
  t(K) = 2*(K - 1);
end
for K = 1:length(t)
  trev(K) = t(end+1-K);
end

etc.

0 Comments

Walter Roberson

Walter Roberson (view profile)

Contact us