how to swap this matrix

 Accepted Answer

Andrei Bobrov
Andrei Bobrov on 17 Oct 2013
Edited: Andrei Bobrov on 17 Oct 2013
Z([1,2]) = Z([2,1]);
ADD
Z = [2
4
2
1
7
9
1
1]; % new case
a = [1, 2]; % replacement value
[l,ii] = ismember(Z,a);
Z(l) = a(3-ii(l));

2 Comments

Anusha
Anusha on 17 Oct 2013
how to swap 1st and 3rd position
Z([1,3]) = Z([3,1]);

Sign in to comment.

More Answers (1)

dpb
dpb on 17 Oct 2013
Z(1:3)=flipud(Z(1:3));
Only works because of odd number, of course. In general to swap individual elements will still take the temporary to hold the one element while overwrite the other.

Categories

Asked:

on 17 Oct 2013

Edited:

on 17 Oct 2013

Community Treasure Hunt

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

Start Hunting!