Clear Filters
Clear Filters

Obtain 2D Histogram from 3D matrix and 2D pdf

1 view (last 30 days)
Hello,
I've got a 2D matrix of values
nK=10; nM=100; nZ=50;
Cz = rand(nK,nM,nZ); % list of values depends on 3 quantities
that I want to histogram with edges
CzEdges = [0:0.1:1]; % where values in the 3D Matrix should be hist'd
Each element of the Cz has a corresponding probabilty defined in a matrix
p_K_M = rand(nC,nK); % 2D pdf, (please never mind the normalization) NB: sum(p_C_M,'all') = 1
p_K_M = p_K_M/sum(p_K_M,'all'); % normalized
that is indepent of the variable (z).
I would like a 2D histogram/pdf of the Cz's but would like to avoid looping and masking as in;
P_Cz = zeros(icz,iz); % the 2D pdf = fxn of only 1 original variable, (and another the Cz values themselves i.e. the histogram Edges)
for iz=1:nZ
thisCz = squeeze(Cz(:,:,iz)); % get the values at the z
for icz=1:length(CzEdges)-1
% get indices where Cz is in correct bin
hasCorrectCz = ( CzEdges(icz) < thisCz ) & ( thisCz < CzEdges(icz+1) );
if ~any(hasCorrectCz)
continue
end
% "weigh" each index with corresponding value from 2D pdf
P_Cz(icz, iz) = sum( p_K_M(hasCorrectCz) );
end
end
Is it possible to use hist3 (I dislike the inputs and I don't think they map well to my variabels as they're constructed), or accumarray to dot times the Cz's with the 2D pdf (p_K_M) or perhaps a generalized histcounts and dot times of a repmatted p_K_M?
I feel like I'm missing a neat function that makes this go from 2 loops to a ~3 liner or something. I really appreciate any tips you have. Please let me know if I've made an error (I "translated" my variables to something easier to parse, but may have done something stupid.)
Cheers,
Michael B.
  2 Comments
Michael Bowles
Michael Bowles on 28 Jul 2020
Edited: Michael Bowles on 28 Jul 2020
I had a bit of an epiphany and believe I'm much closer to the answer but still ened a little push.
Since I aim to 2D histogram the Czs as a fucniton of Cz and z I can use repmat to expand the probability matrix
threeDimJointPDF = repmat(p_K_M,[1 1 nZ]);
and then do the saem for the z's
[~,~,threeDimZ] = ndgrid([1:nK],[1:nM],z); % the values only matter for the z, just need correct dimensions
Now I want to use accumarray or use do a weighted histogram for the splayed out lists!
Michael Bowles
Michael Bowles on 28 Jul 2020
Edited: Michael Bowles on 28 Jul 2020
With the list of Zs Czs and Ps, I'd like to use predefiend Cz and Z edges e.g.
zEdges = [0:80]; % and the CzEdges from before
CzEdges=[0:0.1:1]; % to sum the threeDimJointPDF (third column of the)
allZs_Czs_Ps = [threeDimZ(:) Cz(:) threeDimJointPDF(:)];

Sign in to comment.

Answers (1)

Bruno Luong
Bruno Luong on 28 Jul 2020
Check this out
  1 Comment
Michael Bowles
Michael Bowles on 28 Jul 2020
This is neat. I'm sort of hoping for something a little more transparent (or really native). I suppose I'll accept if no one else chimes in. Thank you.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!