MATLAB: How to display averages in new array

arrayduplicate postmatrix arraymean

Hello, I want to average each column in 'B' and arrange the averages in an array so that each row of the new array will display the set of averages relating to the final stock price B(n+1,i).
Thats where I am having difficulty.Thank you for your efforts
n=4
i=1:16
B =
Columns 1 through 11
100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000
90.0000 90.0000 90.0000 90.0000 110.0000 90.0000 90.0000 90.0000 110.0000 110.0000 110.0000
81.0000 81.0000 81.0000 99.0000 99.0000 81.0000 99.0000 99.0000 99.0000 99.0000 121.0000
72.9000 72.9000 89.1000 89.1000 89.1000 89.1000 89.1000 108.9000 89.1000 108.9000 108.9000
65.6100 80.1900 80.1900 80.1900 80.1900 98.0100 98.0100 98.0100 98.0100 98.0100 98.0100
Columns 12 through 16
100.0000 100.0000 100.0000 100.0000 100.000
110.0000 110.0000 110.0000 110.0000 110.000
121.0000 121.0000 121.0000 121.0000 121.000
108.9000 133.1000 133.1000 108.9000 133.100
119.7900 119.7900 119.7900 119.7900 146.410
Each column in B is a price trajectory of an asset, the asset moves up or down at each row, but some paths end up at the same point after n+1 time steps.
so I have multiple price paths ending at the same point.
I want to average the paths and group them together such that I will have a set of averages relating to one final stock price.
for example my new array A will look like
so A will look something like this
A=
81.9020 0 0 0 0 0
84.8100 88.0500 91.6500 95.6500 0 0
91.6200 95.2200 99.1820 99.2200 103.1800 107.5800
103.5300 107.5300 111.9300 116.7700 0 0
122.1000 0 0 0 0 0
….. so each row of A displays the set of averages for each price path ending at the same point. row 1 of A has the averages for final stock price 65.61 row 2 of A has the averages for final stock price 80.19 for 2 of A has the averages for final stock price 98.01

Best Answer

OK, I changed it from a cell array to a double array like you wanted:
B=[100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.000;
90.0000 90.0000 90.0000 90.0000 110.0000 90.0000 90.0000 90.0000 110.0000 110.0000 110.0000 110.0000 110.0000 110.0000 110.0000 110.000;
81.0000 81.0000 81.0000 99.0000 99.0000 81.0000 99.0000 99.0000 99.0000 99.0000 121.0000 121.0000 121.0000 121.0000 121.0000 121.000;
72.9000 72.9000 89.1000 89.1000 89.1000 89.1000 89.1000 108.9000 89.1000 108.9000 108.9000 108.9000 133.1000 133.1000 108.9000 133.100;
65.6100 80.1900 80.1900 80.1900 80.1900 98.0100 98.0100 98.0100 98.0100 98.0100 98.0100 119.7900 119.7900 119.7900 119.7900 146.410]
% Extract the last row.
lastRow = B(end, :)
[rows, columns] = size(B)
% Get unique prices in the last row, for example [65.61, 80.19, 98.01]
pricesToSearchFor = unique(lastRow)
for k = 1 : length(pricesToSearchFor)
thisPrice = pricesToSearchFor(k);
fprintf('Searching all columns for price $%.2f.\n', thisPrice);
% Search every column for this unique price.
columnCounter = 1;
for column = 1 : columns
thisColumn = B(:, column);
% Find out if this price occurs EXACTLY in the column
lastIndexInColumn = find(thisColumn == thisPrice, 1, 'last');
if ~isempty(lastIndexInColumn)
fprintf('Found %.2f at row %d of column %d of B.\n', ...
thisPrice, lastIndexInColumn, column);
% If we found the price in this column, add it to a new column of A
A(k, columnCounter) = mean(thisColumn(1:lastIndexInColumn));
% Increment the column in A
columnCounter = columnCounter + 1;
end
end
end
A
What it shows is:
A =
81.902 0 0 0 0 0
84.818 88.058 91.658 95.658 0 0
91.622 95.222 99.182 99.222 103.18 107.58
111.94 116.78 116.78 111.94 0 0
122.1 0 0 0 0 0
Is this what you want?