Mapping Out and Multiplying Elements in Vectors

I have three matrices:
Price:
[ 24 29 55;
30 40 56;
19 33 62 ]
Volume:
[100 350 222;
290 300 234;
80 219 180 ]
Grades:
[1 2 3;
1 2 3;
2 3 2 ]
A Column Vector with Stock Symbols:
[ ABX CDF TTQ ]
A Row Vector with Dates:
[040116;
040116;
040216]
I need to calculate the average trading prices of certain securities grouped by their respective grade (which may vary over time). The output I am attempting to create is similar to:
[ 1 2 3 ]
[040116; [28.46 34.08 55.51;
040216] 0.00 48.77 33.00]
I am a Python/SAS developer recently assigned to a MATLAB project so I am new to the language. Any assistance would be greatly appreciated.

回答 (1 件)

Nikhil Vyas
Nikhil Vyas 2016 年 4 月 26 日

0 投票

In your expected output matrix, where is the row [0.00 48.77 33.00] coming from?
Other than that, you can find the average using the mean function.
For the price matrix, you can find the mean directly as following:
pr_mean = mean(Price);
This would return you a Row with the means of the three columns, which is the default behavior.
In case you wanted to find the mean of the Rows, you could do this:
pr_mean_c = mean(Price');
Now, to use the Obtained rows in the output matrix specified by you, you can use the matrix concatenation operations.
To extract certain rows and columns from a matrix, go through the following link:
Once you have the rows / columns you require, it's just a matter of concatenating them. For this, I strongly suggest you go through the following documentation link:
Just remember: ',' to concatenate horizontally and ';' to concatenate vertically. :)

1 件のコメント

Robert Garrison
Robert Garrison 2016 年 4 月 26 日
Thanks for your assistance. I am looking for the weighted average price of a security pertaining to its grade. I know in SAS its a transpose procedure and in Python its a pivot/cross-tabulation procedure. The second row of:
[ 1 2 3 ]
[040116; [28.46 34.08 55.51;
040216] 0.00 48.77 33.00]
is the weighted average price of the securities based upon their grade inferred by the grade ranking. On 04-02-16, there were $0 of grade 1 securities traded, $48.77 of grade 2 securities traded and $33 of grade three securities traded. Again you assistance would be greatly appreciated.

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

カテゴリ

ヘルプ センター および File ExchangeFinancial Toolbox についてさらに検索

製品

質問済み:

2016 年 4 月 22 日

コメント済み:

2016 年 4 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by