Create a Vector based on the Value of another Vector

46 ビュー (過去 30 日間)
Riccardo Della Villa
Riccardo Della Villa 2022 年 1 月 28 日
コメント済み: Voss 2022 年 1 月 31 日
Hi,
i have an issue at creating a vector based on the values of another vector. For the sake of abetter understanding, I post here the code which I would like to edit. I have all of my data. I am rettrieving data from an excel file where they are listed along columns and reflect data on different values, based on the value of PERMNO. At one point in the column, the value of PERMNO changes. So I would also like to define different variables Date and Price depending on the value of PERMNO as of the following:
[data, Mname,raw] =xlsread('trial.xlsx','trial','A1:C3309');
save('bubble.mat','raw');
load('bubble.mat');
data = raw;
permno = cell2mat(data(2:end,1));
date = datenum(cell2mat(data(2:end,2)),'dd/mm/yyyy');
price = cell2mat(data(2:end,3));
Any suggestion on how this can be achieved?
  3 件のコメント
Riccardo Della Villa
Riccardo Della Villa 2022 年 1 月 28 日
編集済み: Matt J 2022 年 1 月 28 日
I am not getting errors, I am just wondering how this can be implemented in an automated manner, without the need to continuously create vectors depending on the PERMNO value.
this is what I have tried to do, but it is definitely not efficient:
[data, Mname,raw] =xlsread('trial.xlsx','trial','A1:C3309');
save('bubble.mat','raw');
load('bubble.mat');
data = raw;
permno = cell2mat(data(2:end,1));
date = datenum(cell2mat(data(2:end,2)),'dd/mm/yyyy');
price = cell2mat(data(2:end,3));
A = [permno date price];
date1 = date(permno == 93434);
price1 = price(permno == 93434);
date2 = date(permno == 93436);
price2 = price(permno == 93436);
My goal is to create a series of vectors like what I have called date1 price 1 date2 price 2, but automating them depending on the PERMNO value.
Ankit
Ankit 2022 年 1 月 28 日
you can create based on some conditions? But you want to create the price and date vectors based on certain conditions right? Is it possible to tell how your data looks like (size, values etc)?

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

採用された回答

Voss
Voss 2022 年 1 月 28 日
u_permno = unique(permno);
N = numel(u_permno);
date_vectors = cell(1,N);
price_vectors = cell(1,N);
for ii = 1:N
idx = permno == u_permno(ii);
date_vectors{ii} = date(idx);
price_vectors{ii} = price(idx);
end
  2 件のコメント
Riccardo Della Villa
Riccardo Della Villa 2022 年 1 月 31 日
Thanks, this works. Once I have defined the price_vectors {ii} can I further perform other operations, let's say on vector y = price_vectors{ii}?
Voss
Voss 2022 年 1 月 31 日
Absolutely. For instance, if you want to find the average price when permo == 93434, you could say:
idx = find(u_permno == 93434);
avg_price = mean(price_vectors{idx});
Or equivalently but more concisely:
avg_price = mean(price_vectors{u_permno == 93434});
Of course, you don't need to have created the price_vectors for that, because you can just operate on the original variables:
avg_price = mean(price(permno == 93434));

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFinancial Data Analytics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by