How do I reverse the order of a vector?

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..

답변 (9개)

Andreas Goser
Andreas Goser 2011년 2월 9일

4 개 추천

I think you are looking for commands like flipud and fliplr, right?
Jan
Jan 2011년 2월 9일

4 개 추천

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

Walter Roberson
Walter Roberson 2011년 2월 9일
That can be expressed more simply:
P = [0 1 7 6
2 3 5 4
4 5 3 2
6 7 1 0];
Jan
Jan 2011년 2월 9일
I think this is more prone to errors and confuses beginners.
Walter Roberson
Walter Roberson 2011년 2월 10일
I think of it as a way of differentiating between those who learn and those who copy without understanding.
Jim Dowd
Jim Dowd 2011년 2월 10일
Funny stuff Jan Simon!

댓글을 달려면 로그인하십시오.

Matt Fig
Matt Fig 2011년 2월 10일

2 개 추천

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

Walter Roberson
Walter Roberson 2011년 2월 10일
I was working on fitting it to a 100'th order polynomial, but discovered that any order beyond 23 gives absolutely completely wrong answers!

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 2월 9일

1 개 추천

t = (0:2:6).'; P = [t, t+1, 7-t, 6-t];
Matt Fig
Matt Fig 2011년 2월 10일

1 개 추천

Or, a one liner:
reshape(permute(reshape([0:7 7:-1:0],2,4,2),[2,1,3]),4,4)
Walter Roberson
Walter Roberson 2011년 2월 9일

0 개 추천

t = (0:2:6).';
trev = t(length(t):-1:1);
P = [t, t+1, trev+1, trev];
Walter Roberson
Walter Roberson 2011년 2월 9일

0 개 추천

t = (0:2:6).'
trev = zeros(size(t));
trev(end:-1:1) = t;
P = [t, t+1, trev+1, trev];
Walter Roberson
Walter Roberson 2011년 2월 9일

0 개 추천

for K = 1 : 4
t(K) = 2*(K - 1);
end
for K = 1:length(t)
trev(K) = t(end+1-K);
end
etc.
Padala Bhaskara Rao
Padala Bhaskara Rao 2018년 12월 6일

0 개 추천

clc;
clear all;
close all;
x=zeros(4,4);
x(:,1)=[1 2 -1 -2];
l=length(x);
x(:,2)=[1 3 -1 -3];
k=1;
for i=3:4
for j=1:4
x(j,i)=x(l+1-j,k);
end
k=k+1;
end

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2011년 2월 9일

댓글:

2020년 11월 5일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by