필터 지우기
필터 지우기

How can I swap two columns of a matrix in MATLAB?

조회 수: 273 (최근 30일)
Deepak
Deepak 2019년 6월 6일
댓글: Walter Roberson 2023년 2월 25일
How can I swap two columns of a matrix in MATLAB?
  댓글 수: 2
David Sinex
David Sinex 2022년 10월 3일
In a single line using fliplr() and given 2 indices idx1 & idx2,
idx1 = 2;
idx2 = 5;
AA(:,[idx1,idx2]) = fliplr(AA(:,[idx1,idx2]));
Walter Roberson
Walter Roberson 2022년 10월 3일
idx1 = 2;
idx2 = 5;
AA(:,[idx1,idx2]) = AA(:,[idx2,idx1]);

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

답변 (1개)

Manvi Goel
Manvi Goel 2019년 6월 6일
There is an easy way to extract a column of a matrix in MATLAB
Suppose you have a matrix A:
A = [1, 2, 3 ; 4, 5, 6]
Screen Shot 2019-06-07 at 12.15.44 AM.png
and you want to swap its first and the second columns.
The following can be done by extracting the first column, storing its value in a temporary variable and replacing second's value with the stored value:
v = A(:, 1);
A(:, 1) = A(:, 2);
A(:, 2) = v;
Screen Shot 2019-06-07 at 12.17.19 AM.png
  댓글 수: 5
Andrea
Andrea 2023년 2월 25일
thanks! helped me as well. I hoped to take the product between the original matrix and a simple binary matrix which would perform the transformation but in my case the matrix is text cell so linear algebra operations are not possible (unless I am missing something)
Walter Roberson
Walter Roberson 2023년 2월 25일
It is not clear what 0 or false times a text entry would be ? Are you hoping, for example, that
false * "hello"
would give a result of "0" ?

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by