How do I create dummy variables with if statements?

I have a dataset with excess return, R, which consists of 10 000 rows and 1 column.
I want to create a new variable X that takes the value of 1 for each row in R if R>0 for all rows in R. And it takes the value of 0 if R<0.
Probably a simple question, but couldnt find any other answers out there that worked for me.

回答 (1 件)

madhan ravi
madhan ravi 2020 年 11 月 6 日
編集済み: madhan ravi 2020 年 11 月 6 日

0 投票

X = R > 0

5 件のコメント

Walter Roberson
Walter Roberson 2020 年 11 月 6 日
X = nan(size(R));
X(R > 0) = 1;
X(R < 0) = 0;
The nan is needed because the user did not define the result for R == 0 exactly.
madhan ravi
madhan ravi 2020 年 11 月 6 日
Thank you sir Walter
Tobias M
Tobias M 2020 年 11 月 6 日
Thank you! This worked.
Tobias M
Tobias M 2020 年 11 月 6 日
Suppose I now have generated 10000 rows with random numbers ranging within (0,1). From the code you provided me with, I have generated 10000 rows with dummy variables.
Dummy value = 1 signifies a positive return, while 0= negative return. Suppose I have p=60% for predicting that return will be positive, and p=80% that return will be negative.
I have generated random numbers using c=rand(10000,1). If dummy variable is 1, and the value in c > 0.6, I have made a "wrong" prediction, c<0.6 signifies a correct prediction. How can i compute returns that take value of market return if i made correct prediction if dummy=1, and value of risk-free return if I made a wrong prediction, c>0.6, and vice versa if dummy=0?

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

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

質問済み:

2020 年 11 月 6 日

コメント済み:

2020 年 11 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by