How to replace some elements from a vector using if

5 ビュー (過去 30 日間)
angelavtc
angelavtc 2016 年 5 月 6 日
コメント済み: angelavtc 2016 年 5 月 6 日
Hello everyone!
If i have a vector, lets say L=[10;10;10;11;11;13;13] which is associated to another vector X=[1;6;65;34;21;73;14] and I want to create a third vector, Z, with almost all the elements in X, but just replacing a 0 in X when the element (i,j) from L changes. Lets say that the result that I want should look like this Z=[1;6;65;0;21;0;14]
Any ideas how to solve this?
I would be really thankful :)

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 6 日
[~,jj]=unique(L)
out=X;
out(jj(2:end))=0
  1 件のコメント
angelavtc
angelavtc 2016 年 5 月 6 日
Thanks Azzi but this is giving me the next vector
Out=[1;6;65;34;0;73;0] and not Z=[1;6;65;0;21;0;14]
any suggestions?
Thanks

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

カテゴリ

Help Center および File ExchangeColorbar についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by