Value swapping

1 vue (au cours des 30 derniers jours)
developer
developer le 8 Sep 2011
Hello,
I have a matrix
x =
7 8
7 13
1 7
3 7
i want to search all 7 and shift to the first column and the corresponding value to the other coloumn, like
x =
7 8
7 13
7 1
7 3

Réponse acceptée

Grzegorz Knor
Grzegorz Knor le 8 Sep 2011
x(x(:,2)==7,:) = fliplr(x(x(:,2)==7,:))
  2 commentaires
developer
developer le 8 Sep 2011
Thanks :)
Grzegorz Knor
Grzegorz Knor le 8 Sep 2011
similar solution:
x(x(:,2)==7,end:-1:1) = x(x(:,2)==7,:)

Connectez-vous pour commenter.

Plus de réponses (1)

Paulo Silva
Paulo Silva le 8 Sep 2011
x = [ 7 8
7 13
1 7
3 7]
f2=find(x(:,2)==7)+size(x,1);
f1=find(x(:,1)~=7);
tmp1=x(f1);
x(f1)=x(f2);
x(f2)=tmp1;
x
Grzegorz Knor solution seems to be better, I didn't test it.
  1 commentaire
developer
developer le 8 Sep 2011
Thanks :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by