How to display averages in new array?
15 views (last 30 days)
Show older comments
michaelsmith
on 18 Mar 2018
Commented: Image Analyst
on 18 Mar 2018
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
3 Comments
Image Analyst
on 18 Mar 2018
My code below (and in your duplicate question) computes the average of everything from row 1 to the current row. Is that what you want? Because I'm not seeing how the average of any values there equals 65.61. For example, all elements in the first column of B are more than 65.61 so how can the mean be that?
Accepted Answer
Image Analyst
on 18 Mar 2018
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?
0 Comments
More Answers (4)
David Fletcher
on 18 Mar 2018
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]
result=zeros(5,16)
for iter=1:5
result(iter,:)=mean(B(1:iter,:))
end
I think I understand (maybe)
5 Comments
Image Analyst
on 18 Mar 2018
Did you see my second answer I just posted? The answer is a cell array because some columns of B may not have that exact price in it.
Image Analyst
on 18 Mar 2018
It's just as confusing as your prior, duplicate question. Do you perhaps mean
B =[...
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 ]
counts = cumsum(ones(size(B)))
cB = cumsum(B)
meanPrices = cB ./ counts
4 Comments
Image Analyst
on 18 Mar 2018
See if this does what you want:
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
celldisp(A)
0 Comments
Stephen23
on 18 Mar 2018
Edited: Stephen23
on 18 Mar 2018
This is easy in just a few lines.
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];
Method one: accumarray:
M = mean(B,1);
[U,~,R] = unique(B(end,:));
C = cell2mat(arrayfun(@(r)1:nnz(R==r),unique(R),'uni',0));
A = accumarray([R(:),C(:)],M(:),[],[],0)
giving:
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
Method two: sub2ind:
[U,~,R] = unique(B(end,:));
C = cell2mat(arrayfun(@(r)1:nnz(R==r),unique(R),'uni',0));
A = zeros(max(R),max(C));
A(sub2ind(size(A),R,C)) = mean(B,1)
giving:
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
It seems that the question shows incorrect values for the fourth row.
1 Comment
Image Analyst
on 18 Mar 2018
Yeah, it seems like it. Glad that your answer corroborated my answer, so it seems like we're right.
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!