How do you add up data in a for loop to plot in a histogram?
    4 views (last 30 days)
  
       Show older comments
    
    Andrew Stark
 on 28 Nov 2020
  
    
    
    
    
    Answered: Walter Roberson
      
      
 on 28 Nov 2020
            n=100;
[Zf_S, Zf_L] = cellfun(@(B) bounds(B, 'all'), Zf);
Zf_max = max(Zf_L);
Zf_min = min(Zf_S);
Zf_vals = linspace(Zf_min, Zf_max, n);
[Df_S, Df_L] = cellfun(@(B) bounds(B, 'all'), Df);
Df_max = max(Df_L);
Df_min = min(Df_S);
Df_vals = linspace(Df_min, Df_max, n);
for i=1:1
    ZZ = Zf{i};
    DD = Df{i};
    Zfr = discretize(ZZ, Zf_vals);
    Dfr = discretize(DD, Df_vals);
    zm = accumarray([Dfr Zfr], 1, [n n]);
    zmf{i}=zm;
end
 surf(Zf_vals, Df_vals, zmf);
This yields the error
Error using surf (line 71)
Z must be a matrix, not a scalar or vector.
Error in Analyze (line 236)
 surf(Zf_vals, Df_vals, zmf);
What is the best way to add up all the zms so that I can plot all of the data in one histogram plot?
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 28 Nov 2020
        sum_zm = sum(cat(3, zfm{:}),3);
surf(Zf_vals, Df_vals, sum_zm, 'edgecolor', 'none');
This is not the best way: the best way is just to keep a running total without recording it in a cell array.
0 Comments
More Answers (0)
See Also
Categories
				Find more on Histograms 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!