How to change axis size without affecting bin size

n=100;
for i=1:3000
ZZ=Zf{i};
DD=Df{i};
Zf2=linspace(min(ZZ(:)),max(ZZ(:)),n);
Df2=linspace(min(DD(:)),max(DD(:)),n);
Zfr=interp1(Zf2,1:numel(Zf2),ZZ,'nearest');
Dfr=interp1(Df2,1:numel(Df2),DD,'nearest');
zm=accumarray([Dfr Zfr],1,[n n]);
surf(zm);
hold on
end
This code generates the following histogram
How do I modify the x and y axis to make them go up until the max of Dfr and Zfr but still have the number of bins remain 100?

 Accepted Answer

[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);
Now do your interpolation with respect to Zf_vals and Df_vals instead of with respect to a vector that changes each time.
Hint: discretize() instead of interpolation .

9 Comments

Thank you so much!
n=100;
for i=1:1
ZZ=Zf{i};
DD=Df{i};
[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);
Zfr=discretize(Zf_vals,1:numel(Zf_vals),ZZ,'nearest');
Dfr=discretize(Df_vals,1:numel(Df_vals),DD,'nearest');
zm=accumarray([Dfr Zfr],1,[n n]);
surf(zm);
hold on
end
I wrote this and ran it but I encountered the error
Error using discretize (line 155)
Third argument, bin values, must be a vector with length equal to the number of bins, 99.
Error in Analyze (line 190)
Zfr=discretize(Zf_vals,1:numel(Zf_vals),ZZ,'nearest');
How would I adjust the the third input to accomadate for that?
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]);
surf(Zf_vals, Df_vals, zm);
hold on
end
The line surf(Zf_vals, Df_vals, zm); might have to be
surf(Df_vals, Zf_vals, zm);
Thank you so much! I am kind of concerned however, because the banana plot looks like this
while the histogram looks like this
Do you know why it might appear this way and how I might fix it?
As I wrote above,
The line surf(Zf_vals, Df_vals, zm); might have to be
surf(Df_vals, Zf_vals, zm);
Also, I generally recommend using the option 'edgecolor', 'none' for surf.
And remember, you are drawing values on top of values, and the upper-most drawing is going to hide the ones underneath unless you use FaceAlpha less than 1 to allow underlying layers to show through.
Thank you I will look into those options. I forgot to mention that I did use surf(Df_vals, Zf_vals, zm); and it still yielded a different graph than the other plot. Do you know why the histogram graph might look different?
Also I know that running a for loop and holding the plot creates all the layers but how would I make it so that it was one layer with all the data?
You did not happen to describe any relationship between a Banana anything and the values you are taking the histogram of.
You could potentially get one layer with all of the data if you were to sum all of the zm arrays and then after off of the loops, surf the result.
My apologies for the banana plot the following code was used
for i=1:1
Zf2=Zf{i};
Df2=Df{i};
plot(Df2,Zf2)
hold on
end
Whereas for the histogram the code was
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]);
surf(Df_vals, Zf_vals, zm);
hold on
end
Additionally what is the best command to use in a for loop that will continuously add up the zm arrays

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!