Indexing negative and positive elements of a vector

12 ビュー (過去 30 日間)
Marcin
Marcin 2013 年 6 月 24 日
Hello, I want to find a more elegant way of indexing positive and negative elements of a vector. Here is what I have so far. I have a vector of numbers (Vec1) and I would like to find a coresponding vector where the logical indexing assigns 1 to positive numbers and -1 to negative numbers.
e.g.
Vec1 = [-0.2 0.3 0.5 0.7 -0.4 0.5 -0.5]
PosInd = Vec1 > 0;
NegInd = (Vec1 < 0)*(-1);
IndVec = PosInd + NegInd;
IndVec is = [-1 1 1 1 -1 1 -1]
Is there a nicer (and more direct) way to do that?
Thanks in advance,
Martin

採用された回答

Jonathan Sullivan
Jonathan Sullivan 2013 年 6 月 24 日
help sign
doc sign
  1 件のコメント
Marcin
Marcin 2013 年 6 月 24 日
Ouah! Great!!! Thanks so much!
Marcin

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by