swiching elements of same vector

조회 수: 2 (최근 30일)
jean claude
jean claude 2016년 4월 6일
편집: jean claude 2016년 4월 6일
hi everybody, i have vector
X=[ 20; 0; 0;0; 100; 0;0;0;50];
i would like to have
X=[0;0;0;0;20;0;0;0;100];
so that each number take the next number case

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 6일
X=[ 20; 0; 0;0; 100; 0;0;0;50]
ii=find(X~=0)'
idx=circshift(ii,[0 1])
X(ii(2:end))=X(idx(2:end))
X(ii(1))=0
  댓글 수: 1
jean claude
jean claude 2016년 4월 6일
it's good answer thanks very very much

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2016년 4월 6일
편집: Jos (10584) 2016년 4월 6일
Easy:
X = [20 0 0 0 100 0 0 50 0]'
[i,~,v] = find(X)
X(i) = [0 ; v(1:end-1)]
  댓글 수: 1
jean claude
jean claude 2016년 4월 6일
편집: jean claude 2016년 4월 6일
easy way, good !

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by