How to display averages in new array?
Mostrar comentarios más antiguos
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 comentarios
David Fletcher
el 18 de Mzo. de 2018
To be honest, I'm having trouble understanding what this final matrix will look like. If n=4, and i=1:16 does this mean that you want the entire fifth row of B (your stated B(n+1,i)) to be merged with mean(B) giving a 2x16 matrix. If not, some sort of example to clarify what you are trying to achieve might be helpful.
michaelsmith
el 18 de Mzo. de 2018
Image Analyst
el 18 de Mzo. de 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?
Respuesta aceptada
Más respuestas (4)
David Fletcher
el 18 de Mzo. de 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 comentarios
David Fletcher
el 18 de Mzo. de 2018
Editada: David Fletcher
el 18 de Mzo. de 2018
Just seen the above comment, I'm afraid I'm back to be completely in the dark again. Thank Christ I never became a banker.
michaelsmith
el 18 de Mzo. de 2018
David Fletcher
el 18 de Mzo. de 2018
OK, I'll have another look in bit. Just got to go and mop my fevered brow with a copy of the Financial Times
michaelsmith
el 18 de Mzo. de 2018
Image Analyst
el 18 de Mzo. de 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
el 18 de Mzo. de 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 comentarios
michaelsmith
el 18 de Mzo. de 2018
Editada: Stephen23
el 18 de Mzo. de 2018
Image Analyst
el 18 de Mzo. de 2018
Not really. As you can see, you're saying A(1,1) is the mean of B(:, 1). And the first column of B is [100, 90, 81, 72, 65], the mean of which is 81.6. However you say it should be 65.61. Why?
michaelsmith
el 18 de Mzo. de 2018
michaelsmith
el 18 de Mzo. de 2018
Image Analyst
el 18 de Mzo. de 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)
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 comentario
Image Analyst
el 18 de Mzo. de 2018
Yeah, it seems like it. Glad that your answer corroborated my answer, so it seems like we're right.
Categorías
Más información sobre Calendar en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!