swiching elements of same vector
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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개)
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)]
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!