Value swapping

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

 采纳的回答

x(x(:,2)==7,:) = fliplr(x(x(:,2)==7,:))

2 个评论

Thanks :)
similar solution:
x(x(:,2)==7,end:-1:1) = x(x(:,2)==7,:)

请先登录,再进行评论。

更多回答(1 个)

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.

类别

帮助中心File Exchange 中查找有关 Image Processing Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by