swap columns of a matrix
조회 수: 129(최근 30일)
표시 이전 댓글
Hello guys/girls
How do i swap columns??
I have this
k_minus =
-46 -43 -26 -14 7 19 11 32 39 45 45
0 -4 -7 -7 -44 -44 -7 -7 -15 -15 0
and I want the columns to be in opposite order - How do I do this? and can do this in one go?
댓글 수: 1
Keerthi Krishna PARVATHANENI
2021년 9월 15일
Easy way is to swap the coloums based on the positon
For example:
A=[10 20 30;
40 50 60];
swap the coloum 1 to 2 can be done by
A(:,[2 1 3])
ans =
20 10 30
50 40 60
Exact approach can be adopted ot swap rows
A([2 1],:)
ans =
40 50 60
10 20 30
I think, this can be easy way with minium code.
Enjoy :)
추가 답변(2개)
Mischa Kim
2014년 2월 25일
편집: Mischa Kim
2014년 2월 25일
Use
k_minus_rev = k_minus(:, [length(k_minus(1,:)):-1:1])
댓글 수: 4
kk
2019년 4월 2일
Thank you for this answer! It has helped me "merge" two matrices, i.e. creating a matrix consisting of the first column of matrix A, first column of matrix B, second column of matrix A, second column of matrix B, etc.
rishabh gupta
2018년 1월 12일
you can also use: k_minus_rev = k_minus(:, [length(k_minus):-1:1])
댓글 수: 2
참고 항목
범주
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!