필터 지우기
필터 지우기

Value swapping

조회 수: 1 (최근 30일)
developer
developer 2011년 9월 8일
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

채택된 답변

Grzegorz Knor
Grzegorz Knor 2011년 9월 8일
x(x(:,2)==7,:) = fliplr(x(x(:,2)==7,:))
  댓글 수: 2
developer
developer 2011년 9월 8일
Thanks :)
Grzegorz Knor
Grzegorz Knor 2011년 9월 8일
similar solution:
x(x(:,2)==7,end:-1:1) = x(x(:,2)==7,:)

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

추가 답변 (1개)

Paulo Silva
Paulo Silva 2011년 9월 8일
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
developer
developer 2011년 9월 8일
Thanks :)

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

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by