MATLAB Surf plot with bins - matlab

I have a nxm matrix where n is the number of bins in the x axis and m is the number of bins in the y axis. Each position in the matrix has a number which shows how many data points are in matching x-y bin ranges from my raw data and when surf plotted results in a density plot.
The problem is if I have 100 x-bins and 50 y-bins the axis of my surf plot will be 0-100 on the x-axis and 0-50 on the y-axis. Which makes sense because I am doing surf(Matrix).
But infact I want the x and y axis to be from 0-5 for example. Therefore the number of bins for x and y will determine only the resolution of the plot. Low number of bins = low resolution. High number of bins = high resolution. When for the whole time the axis of the plot are 0-5.
In a nutshell is it possible to plot a matrix of varying size that is used for a surf plot into pre-assigned axis values?
(Of course doing axis([0 5 0 5]) will not work)
Found it difficult to word the question but hopefully it makes sense.

Related

plotting data between concentric circles of known radii

I have a vector of 1000 random numbers biased towards the bounds of 540 and 600. I need to plot this data as a wriggly circular path between two concentric circles of radii 540 and 600 respectively. How do I do this?
Presently, I'm able to plot the concentric circles of given radii, but if I try to plot the given random data which is between the bounds 540 and 600, it is plotted along the width between the two concentric circles. I want it to be plotted as a noisy circular curve between the concentric circles.
I hope I'm able to explain my point
If anyone can tell me, how do I do that. Thanks
Here is the link to my previous post, wherein I had to generate random numbers biased towards the two well defined bounds
Generating random numbers in matlab biased towards the boundaries
Now I need to plot the same data, as explained above.
This is the image I get:
What you actually want is a circular plot, so the first idea that comes to my mind is to use polar coordinate.
First we get your sample of around 1000 random datas biased to the bounds of your [540 600] interval following that.
Note that this algorithm will not always get you exactly 1000 values as the out of bound values are removed.
So we'll do something like this :
%Get the size of the sample
P=length(X);
% Generate P Angles uniformly distributed between 0 and 2*pi*(P-1)/P
% Note generating an angle equal to 0 and an angle equal to 2*pi would
% mean that we have 2 points with the same angle, and that s something
% you generally want to avoid
Angles=(0:P-1)*(2*pi/P);
% Plot your points with the markers (polar coordinates) in '-+',
% - means that you want a line to be drawn between the points and
% + means that you want a + marker on every point
plot(X.*cos(Angles),X.*sin(Angles),'-+');
%tell matlab to wait so you can add the circles
hold on
% add circle of 540 radius
plot(540*cos(Angles),540*sin(Angles),'-r');
%add circle of 600 radius
plot(600*cos(Angles),600*sin(Angles),'-r');
For the final result of :
Update : About getting the angles randomly distibuted
We'll have to define our Angles diffrently. We still need a vector of length P, but randomly distributed. This can be achieved by taking P random numbers between 0 and 1, and then multiply this vector by 2*pi in order to get randomly distributed values between 0 and 2*pi.
Last step is to order this vector in order to keep the ordering of your data.
Temp=rand(1,P)*2*pi;
Angles=sort(Temp);
For a final result of :
Another lead woult be to take a sample of 1000 gaussian random numbers and use them modulo 2*pi :
Angles=sort(mod(randn(1,P)*2*pi,2*pi));

Matlab: Multiply vertical axis of hist plot by a number

The hist function in matlab plots the frequency of the data points in the vertical axis. What should I do if I want to multiply the frequency by a number.
For example if an object points at North 10 times during the entire recording of data and each time, it stays in the North position for 5 seconds. Instead of having the hist plot show that the object pointed at North 10 times, I would like it to show that it pointed at North for a total of 50 seconds.
You can use the histc (or histcounts in 2014b) function to do the bin counting without plotting the data, then plot using bar.
directions = rand(1000, 1)*360;
% note that histc counts the last element as exactly equal, so I pad
edges = [0:5:360, inf];
counts = histc(directions, edges);
multiplier = 5;
bar(edges(1:end-1)*multiplier, counts(1:end-1))

MATLAB contour plot of 2D scatter

What I want to do is very simple, I just cannot seem to get MATLAB to do it. I would like to plot contours using my 2D data set.
My data set is large; 2 x 844240. I can do a scatter plot just fine,
scatter(Data(1,:), Data(2,:));
Reading through the forums I found Scatter plot with density in Matlab, where a hisogram was plotted. This would suffice, however, I would like to overlay the plots.
The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; whereas the histogram is [0 100 0 100].
Is there a way to change the axis values of the histogram without modifying the image?
Thanks!
If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc, e.g.
nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)
A couple other notes:
In your case, you will need to transpose your [2 x N] matrix when passing it to hist3, which expects an [N x 2] matrix.
imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. If you want to flip it, you can use:
imagesc(y_bin_centers, x_bin_centers, counts')
If you want to specify the histogram bins explicitly (e.g. to match your scatterplot) you can specify that in the arguments to hist3:
x_bin_centers = linspace(0, .01, 100);
y_bin_centers = linspace(0, 2500, 100);
counts = hist3(Data, {x_bin_centers, y_bin_centers};
And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc):
contour(x_bin_centers, y_bin_centers, counts');
If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation).

Matlab - plot image gradients using vector representation

I have computed the gradients from every pixel location of a grayscale image, both on X and Y axis and this can result in a vector representation for each pixel location. I want to obtain a plot figure similar to the one illustrated bellow:
My image has 1000 x 1002 dimensions and I have computed the gradients for each pixel on X and Y directions so I have 2 matrices, each one having 1000 x 1002 dimensions.
I am interested in obtaining a plot similar to the one illustrated in the image above, where I show basically the direction of each vector obtained from the computed gradients. I do not care about the magnitude of the vector, so basically each arrow can have the same length.
Do you know how can I obtain something similar to this?
It works in my case:
[DX,DY] = imgradient(imageIn);
%show gradient
figure;
[x,y]=meshgrid(1:1:500);
figure
quiver(x,y,DX,DY)
hold off

3d plot with given 2d data

I want to understand how the 2d data is related to z axis to get the 3d plots
let us say that i have x=[-1:0.1:1], vector
and y=[1 2 3 4 5 4 3 2 1 0]
a plot of y Vs x will have peak of 5 and slope down to both sides at x=0.5
how to relate these data in 3d to get the bell shape surface, with similar characteristics.
You can view a line/curve plot as a function of a single variable, y=f(x), and typically, x and y are both vectors. For e.g., you can plot the Gaussian bell curve as
x=linspace(-3,3,1000);
y=exp(-x.^2/2);
plot(x,y)
A surface plot, on the other hand, is a function of two variables, z=f(x,y) where x and y can be either vectors or matrices and z is a matrix. meshgrid is a very handy function that generates 2D x and y arrays from 1D vectors by proper replication.
It is the z matrix that you plot either as a 2D image (values of z are represented by colors) or a 3D plot (values of z are represented as heights along the z-axis). For e.g., a 3D Gaussian bell curve can be plotted as
x=linspace(-3,3,1000);y=x'; %'
[X,Y]=meshgrid(x,y);
z=exp(-(X.^2+Y.^2)/2);
surf(x,y,z);shading interp
This is how the respective plots should look like