필터 지우기
필터 지우기

Change column and row

조회 수: 46 (최근 30일)
nur yusof
nur yusof 2016년 2월 22일
댓글: nur yusof 2016년 2월 22일
Hi, how can I change the positions of entire row and columns in a matrix [nxm]? For example, I want to change my entire 10th row and 10th column into entire 1st column and 1st row.
Thank you.

채택된 답변

Dennie
Dennie 2016년 2월 22일
Hello, You can swap columns and rows quite easily in matlab. For example:
given matrix :
matrix=[1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20];
i can swap column 1 and 2 by performing this operation:
matrix(:,[1,2])=matrix(:,[2,1]);
for row 1 and 2, a similar operation is done:
matrix([1,2],:)=matrix([2,1],:);
Please be aware of the sequence in which you perform this operation, since column 10 and row 10 have 1 variable in common, if you swap columns and rows sequentialy, you will mix up that one variable.
Another (manual) approach to retain information is to make a copy of the row and column you want to swap in another variable such as:
copy_matrix=matrix(:,:);
matrix(2,:)=copy_matrix(1,:);
matrix(1,:)=copy_matrix(2,:);
Hope this helps,
Dennie
  댓글 수: 1
nur yusof
nur yusof 2016년 2월 22일
Excellent.Thank you so much.

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

추가 답변 (1개)

Ilham Hardy
Ilham Hardy 2016년 2월 22일
편집: Ilham Hardy 2016년 2월 22일
There are built-in matlab functions for this..
  댓글 수: 2
Ilham Hardy
Ilham Hardy 2016년 2월 22일
For example,
matrix=[1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20];
>> matrix
matrix =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
>> flipud(matrix)
ans =
16 17 18 19 20
11 12 13 14 15
6 7 8 9 10
1 2 3 4 5
>> fliplr(flipud(matrix))
ans =
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1
nur yusof
nur yusof 2016년 2월 22일
Awesome. Thank you so much.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by