swiching elements of same vector

11 Ansichten (letzte 30 Tage)
jean claude
jean claude am 6 Apr. 2016
Bearbeitet: jean claude am 6 Apr. 2016
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

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 6 Apr. 2016
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 Kommentar
jean claude
jean claude am 6 Apr. 2016
it's good answer thanks very very much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jos (10584)
Jos (10584) am 6 Apr. 2016
Bearbeitet: Jos (10584) am 6 Apr. 2016
Easy:
X = [20 0 0 0 100 0 0 50 0]'
[i,~,v] = find(X)
X(i) = [0 ; v(1:end-1)]
  1 Kommentar
jean claude
jean claude am 6 Apr. 2016
Bearbeitet: jean claude am 6 Apr. 2016
easy way, good !

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by