フィルターのクリア

How to flip values greater than zero in a row

1 回表示 (過去 30 日間)
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya 2018 年 4 月 16 日
編集済み: Stephen23 2018 年 4 月 16 日
If I have a row in matrix like that
0 0 0 0 1 2 3 0 0 0
I want to make it like that
0 0 0 0 0 0 3 2 1 0
If it is possible to do?
  1 件のコメント
Stephen23
Stephen23 2018 年 4 月 16 日
@Tanmoyee Bhattacharya: the numbers a not only flipped L-R, but have been translated too. How is the new position determined?

サインインしてコメントする。

採用された回答

Birdman
Birdman 2018 年 4 月 16 日

Try this:

A=[0 0 0 0 1 2 3 0 0 0]
idx=find(A~=0);
A(idx+nnz(A)-1)=fliplr(A(idx));
A(idx(1:end-1))=0
  1 件のコメント
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya 2018 年 4 月 16 日
編集済み: Stephen23 2018 年 4 月 16 日
Thank you so much sir.It is very much useful. It is working for one row perfectly.I have multiple rows Two are
A=[0 0 0 0 0 0.105037027 0.894962973 0 0 0
;0 0 0 0 0 0 0.990679262 0.009320738 0 0
];
It should be like
0
0 0 0 0 0 0.894962973 0.105037027 0 0
0 0 0 0 0 0 0 0.009320738 0.990679262 0
But the result is coming
0 0 0 0 0 0 0 0 0.990679262000000 0.00932073800000000
0 0 0 0 0 0 0 0.894962973000000 0 0
For many rows how to change the code?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by