swiching elements of same vector

6 ビュー (過去 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 Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by