I wanna replacing 2nd and 3rd column to each other. How can I write a command?

example; 1 2 3 2 4 6 3 6 9 2nd 3rd

댓글 수: 1

Please, sermet, format your messages properly. Do you see that it is confusing? There is a "? Help" button on top of the mask to input message. Please read the instructions presented there.
Posting two questions about the same problem is called "double posting" and not liked in forums. Therefore this forum offers the possibility to edit a question in case of unclear formulations.

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

추가 답변 (2개)

Another way:
column2 = m(:,2); % Save old column2.
m(:,2) = m(:,3); % Move col3 to col2.
m(:,3) = column2; % Make col3 the old col2

댓글 수: 4

Jan
Jan 2013년 1월 9일
편집: Jan 2013년 1월 9일
Do you believe, that this is more efficient than my idea? I ask for "believing", not a measurement.
I am going to go with yes (but it is a guess). For m(:, [2,3]) = m(:, [3,2]) I think MATLAB would allocate a brand new n x m array while IA method would allocate a N x 1 array and reuse/modify the n x m array. That said, I would have gone with m(:, [2,3]) = m(:, [3,2]).
I have no idea. For things this tiny I usually don't worry about it. I went with a typical "swap" method. Jan, yours is more MATLAB-ish. For larger arrays, I might time them to compare speeds.
@Image Analyst and Daniel: I believe that Matlab does not allocate an [n x 2] array for the right hand side m(:, [3,2]), but does something like Bruno did in his FEX: MinMaxSelection with the inplace shared subarray tricks. I will perform some tests with large arrays.

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

Assuming I understand the question right, here is an example of how it could be done:
x = magic(5); % a random matrix for illustration.
l = 1:size(x,2); % get the number of columns of x.
l(2:3) = [3 2]; % setup index replacement.
y = x(:,l); % y will have the same elements as x, with rows 2 and 3 exchanged.
helps?

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2013년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by