필터 지우기
필터 지우기

swap elements in a matrix

조회 수: 51 (최근 30일)
Giannakis Stoukas
Giannakis Stoukas 2015년 3월 20일
댓글: the cyclist 2018년 5월 10일
Hello i want to swap elements in a matrix.I want to do it to elements of the same row and to elements of differents rows. Please help.Thank you
  댓글 수: 2
Adam
Adam 2015년 3월 20일
An example would be useful, although the usual method for swapping elements is to use a temporary variable to store one while you copy the other over the top of it and then copy from the temporary variable into the 2nd location.
Giannakis Stoukas
Giannakis Stoukas 2015년 3월 20일
편집: the cyclist 2015년 3월 20일
To be more specific i want to execute local search to a matrix like this one
A=[1 4 7 16 32 48 1;
1 5 6 23 1 0 0;
1 9 31 8 2 1 0;
1 11 19 66 1 0 0]
The the elements of the matrix are arcs(for example 9-31 is the arc between nodes 9 and 31),so i want to make enough exchanges to reduce the cost.I can do exchanges of the same row like the 9 with 31 and i will the 3 row 1 31 9 8 2 1 0 and the other rows as they were,and i can do exchanges of different rows like the node 9 with the node 11. Is there any command to swap,or i have to use temp variables?

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

채택된 답변

the cyclist
the cyclist 2015년 3월 20일
편집: the cyclist 2018년 5월 10일
% A matrix:
A = magic(3);
% Swap the first and second elements of the first column:
A([1 2]) = A([2 1])
% Swap the first and second elements of the third column:
A([7 8]) = A([8 7])
% Swap the first and third elements of the first row:
A([1 7]) = A([7 1])
Note that I needed to take advantage of linear indexing.
  댓글 수: 4
YANAN ZHU
YANAN ZHU 2018년 5월 9일
A([1 7]) = A([7 1]) actually swap the first and third elements of the first row, not "Swap the first and second elements of the first row"
the cyclist
the cyclist 2018년 5월 10일
Oops, there were actually a couple confusing typos in my post. I have edited to correct them. Thanks for pointing that out.

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

추가 답변 (1개)

Ahamed Tuani
Ahamed Tuani 2017년 11월 13일
Hi,
I have been looking into local search as well. Giannakis, I know its quite q while ago but have you figure out how to do it?
The cyclist and fellow members, I have a question..say the path is 1-31-9-2-10-1..I want to swap 9 and 2 so that the new path will be 1-31-2-9-10-1. How can I do this? Tried indexing (maybe a dumb way) i.e init_tour((1:2):(4):(3):6) to get the new tour but its not working. Any help will be greatly appreciated. Thanks.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by