hello_world - counters is more likely a 2x20 matrix as counts1, counts2, centers1, and centers2 are (probably) 1x10 matrices since the default number of bins when using hist is 10. If that is the case, and you want to determine the expectation of X and Y, then I suspect that you have to make use of all of the column data.
Try saving the data to a cell array so that you can maintain the 2x2 matrix (that you originally hoped for)
counters = {counts1, centers1; counts2, centers2};
Now calculate the expectation as
[row, col] = size(counters);
E = zeros(row);
for k = 1:row
E(i) = sum(counters{k,1}.*counters{i,2})/sum(counters{i,1});
fprintf('%f\n', E(i));
end
For each row, we do
E(i) = sum(counters{k,1}.*counters{i,2})/sum(counters{i,1});
where, for each of the ten bins, we multiply the bin count by the bin centre. We sum these values and divide by the sum of all bin counts to get the expectation.
Best Answer