How to use a for loop for concatenated matrices. [counts, centers] = hist(X);

1 Ansicht (letzte 30 Tage)
Hello Friends,
I am trying to do the following:
[counts1, centers1] = hist(X); [counts2, centers2] = hist(Y);
%X and Y both are the n x 1 matrices, i.e., have only one column with real numbers.
counters = [counts1, centers1; counts2, centers2]; %Here, I am trying to make a 2x2 matrix.
Now, I want to compute the expected value as follows:
[row, col] = size(counters);
E = zeros(row);
for i = 1:row
E(i) = (counters(i,1)*counters(i,2)')/sum(counters(i,1));
fprintf('%f\n', E(i)); % Trying to print it on command window.
end
I am not getting expected results. Expected Values E(1) and E(2) should print on command window separately for each sets of counts & centers (let's say we want to print them in column). Please advise. Thanks in advance.
  2 Kommentare
Stephen23
Stephen23 am 15 Feb. 2015
"I am not getting expected results": so exactly what does this code do now? It would be better if you described exactly the desired output, then we could tell you how to achieve this. Give examples, and please format them as code!
hello_world
hello_world am 15 Feb. 2015
Bearbeitet: hello_world am 15 Feb. 2015
Hello Stephen,
Below Geoff Hayes has answered this question. Reading his answer will give more insight of this problem. If possible, you can help me with the second part of the problem (about printing on command window) I asked below to Geoff.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 15 Feb. 2015
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)); % Trying to print it on command window.
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.
  2 Kommentare
hello_world
hello_world am 15 Feb. 2015
Bearbeitet: hello_world am 15 Feb. 2015
Hello Geoff,
Thank you for this answer. It solved my purpose. So I was basically unaware of the use of square bracket & curly braces. Can you please comment on this too, i.e., the difference between '[' & '{' in MATLAB coding?
Secondly, I wrote this code in a function, and calling it from a script file. I will like to print it in command window so that it gives result something like:
Expected Value For the 1st Group:
Index E
1 1.01
Expected Value for the 2nd Group:
Index E
2 2.02
I just took above two random numbers. You were right that each counts and centers are of the size 1x10 by default.
Thank you again for help!
Geoff Hayes
Geoff Hayes am 15 Feb. 2015
The square brackets are used for concatenating elements (scalars, arrays, etc.) together. For example
A = [1 2 3 4];
creates a 1x4 array of the four elements. Whereas
A = [1; 2; 3; 4]
creates a 4x1 array.
The curly braces are used to access elements within a cell array. See cell arrays for more information on this type of array (I typically use this type to manage data of different types or different dimensions). To access elements within a non-cell array (or matrix), you would use the usual brackets ().
To print information to the console, use either fprintf or disp.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by