How can I use one variable to extract specific observations from another variabel?

1 回表示 (過去 30 日間)
I have a variable (A) signalling to buy or sell another variable (B) (if A is negative; sell (positive; buy). What is the easiest way to extract all the "buys" and "sells" from variable B in different datasets (so I have one dataset "Buy" and one dataset "Sell") based on the information in A? For example if A is;
  • -2
  • -1
  • 2
  • 3
And B is;
  • 5
  • 10
  • 8
  • 4
I want to have an ouput of two datasets; "Buy" with 4 and 8, and "Sell" with 5 and 10.
Any help is much appreciated! Best wishes for the new year!

採用された回答

Wayne King
Wayne King 2012 年 12 月 27 日
編集済み: Wayne King 2012 年 12 月 27 日
You can use logical indexing on A to select the appropriate elements of B
A = [-2 -1 2 3];
B = [5 10 8 4];
buyz = B(A>0);
sellz = B(A<0);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSmoothing and Denoising についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by