How to do column or row swapping or interchange in a matrix?

조회 수: 63 (최근 30일)
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2021년 12월 26일
댓글: Joshua 2024년 4월 17일 16:41
Hi,
IFor example, In a matrix A = 3x5 , I want to swap complete column 2 with column 5.
How can i do that. Is there any in-built function or code?

채택된 답변

Voss
Voss 2021년 12월 27일
A(:,[5 2]) = A(:,[2 5]);
  댓글 수: 2
Stephen23
Stephen23 2021년 12월 27일
+1 neat and simple
Joshua
Joshua 2024년 4월 17일 16:41
Thank you for the great comment!

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

추가 답변 (1개)

Yusuf Suer Erdem
Yusuf Suer Erdem 2021년 12월 26일
Try these codes below please;
A = [1, 2, 3, 4, 5; 4, 5, 6, 7, 8; 9, 10, 11, 12, 13]
v = A(:, 2);
A(:, 2) = A(:, 5);
A(:, 5) = v;
A
  댓글 수: 2
SANDEEP SINGH RANA
SANDEEP SINGH RANA 2021년 12월 26일
Thanks u.
I have this idea but searching for some inbuilt function.
Thanks again.
Yusuf Suer Erdem
Yusuf Suer Erdem 2021년 12월 27일
Hello use my codes below please. If this answer does your job, accept my answer please. After my research, i could not find a specific function just for this purpose.
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15];
A
A = A(:,[1 5 3 4 2])

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by