Tracing a line along local maxima in Matlab - matlab

I have a 2D image consisting in the simulation of a band structure 1. I would like to track the maxima on the image to have lines (indicating the bands). The first thing that I did is to calculate the maxima in each line (showed in 2). Then I have a set of points. However, I dont know how to connect each of the points. Does Matlab have a function capable of doing that? Or is there any reasonable way for doing it? So far, I am connecting the points by hand, but that is not scalable.
Many thanks
contourplot showing the raw data
plot showing the maxima of the colorplot in (1)

Related

k-means algorithm for energy data against time and date

I am using Matlab 2015a.
I have got electricity consumption data to cluster it. Initially i am trying to cluster it against hours and dates. I have created three different variables, one for time, one for dates and third for data. I am unable to understand how should i combine these in a matrix form so that the loads are distributed according to time? Then i have tried to look how can i plot a line graph for k-means but i can only find scatter command graphs but no line plots.
Further how can i plot it as a 3-d plot?
Further at a later stage i want to include temperature variable aswel. But when the 4th variable is involved, what will the plot be? will it still be 3-d?
Any suggestions, links?
In Matlab you can create N-dimensional matrices, so you can arrange your 3D data in a N*M*3 matrix (you might want to look for the cat() function to help you out).
There are several functions that allow you to plot in 3D, one of these is scatter3() which is perfect for K-Means clustering. I don't really understand which lines you do want to plot: K-Means is about clusters and centroids (i.e. points).
If a 4th variable is involved, you can as well create a 4D matrix. Although I reckon plotting a 4D graph isn't going to be easy. A first approach might be using several colours for your scatter points with different colours for different temperatures (or temperatures range). In this case the 5th input argument for scatter3() will be helpful.
Help for scatter3() here.
Help for cat() here.

How to reduce size of printed eps when plotting large amounts of data

I am Plotting and printing a large dataset to eps:
plot(Voltage,Torque,'b.')
print -depsc figure.eps
Through these million data points I will fit a graph. However since the sizes of the Voltage and Torque vectors are enormous my eps file is 64.5 MB.
Most of the plotted points however lie on top of other points or very close. How can I reduce the size of the .eps while still having limited effects on the way the data is shown in the graph? Can I make matlab detect and remove data points close enough to other already plotted points?
although it is a scatter plot, I am not using scatterplot since all points should have the same size and color. Is it possible to use scatterplot to remove visual obsolete datapoints?
Beyond stackoverflow, the File Exchange is always a good place to start the search for a solution.
In this case I found the following submissions:
Plot (Big):
This simple tool intercepts data going into a plot and reduces it to the smallest possible set that looks identical given the number of pixels available on the screen.
DSPLOT:
This version of "plot" will allow you to visualize data that has very large number of elements. Plotting large data set makes your graphics sluggish, but most times you don't need all of the information displayed in the plot.
If you end up using the plot in a LaTeX-file, you should consider using
matlab2tikz:
This is matlab2tikz, a MATLAB(R) script for converting MATLAB figures into native TikZ/Pgfplots figures.
For use in LaTeX you don't have to go the detour of PostScript and it will make for beautiful plots.
It also provides a function called: CLEANFIGURE('minimumPointsDistance', DOUBLE,...), that will help you reduce the data points. (Possibly you could also combine this with the above solutions.)
If your vector Voltage is already sorted and more or less regularly spaced, you can simply plot a fraction of the data:
plot(Voltage(1:step:end),Torque(1:step:end),'b.')
with step set to find the right tradeoff between accuracy and size of your eps file.
If needed, first sort your vectors with:
[Voltage,I] = sort(Voltage);
Torque = Torque(I);

Data interpolation over a non-homogeneous surface

In my project i deal with big data surfaces.
In a certain point, i have a line across the data, and I need the values of the points of the line.
The grid is non,homogeneous, it doesnt go from n:m with fixed steps nor nothing.
Lets ilustrate!
In the figure the 2D proyection of my data can be seen. Each of the points has also other 3 data information. I defined a arbitrary red line with the form y=ax+b. a and b are known.
How can I define i.e. 50 points in the line that has not only the x and y coords (wich is straigforward) but also the interpolation of the 3 data information of each of the points around it.
I know is not an easy question but I can't seem to step forward even a bit.
PD: realize I DONT want code written for me, but the idea of how to achieve my objective.
You could use a tool like triScatteredInterp, which will triangulate the 2-d domain, then interpolate a list of points along your line. Griddata is also an option.
I have a toolbox for problems like this (of course.) It allows me to build a triangulation of the non-convex domain in the (x,y) plane. Then it can form a completely general slice through that surface, interpolating in z also as it does so. The result will be a 1-manifold, in this case a piecewise linear function along that path in (x,y,z). While those tools are not posted on the file exchange, they are available for the person willing to invest the time to learn to use them.
If the surface you describe is a completely general one in 3-d, that might be fairly complex, then you might need a CRUST based tool to define that surface triangulation. These can be found online too. Once a triangulation is available, my tools can then be used to slice them. (Sorry, I never did finish that piece.)
What I did was to define several points in the crack line and then cheack for each one of them in wich quadrilateral it is with inpoligon matlab function (no tthe fastest way but less than 2 secs).
Then I created a triangular plane in the used quadrilaterals using x,y and Z or the othre data , achieving a linear interpolation between the data.
finally i take out all the points that are 0 o Nan.

Extract only one spike from matlab plot

I have a matlab plot between two matrices (129 by 1), I just want to extract the only first spike from the multiple spikes as seen in plot. I am newbie to Matlab.
-John
Either code your own or use this peak finder algorithm. That one works very well.

how to make a smooth plot in matlab

I have about 100 data points which mostly satisfying a certain function (but some points are off). I would like to plot all those points in a smooth curve but the problem is the points are not uniformly distributed. So is that anyway to get the smooth curve? I am thinking to interpolate some points in between, but the only way that comes up to my mind is to linearly insert some artificial points between two data points. But that will show a pretty weird shape (like some sharp corner). So any better idea? Thanks.
If you know more or less what the actual curve should be, you can try to fit that curve to your points (e.g. using polyfit). Depending on how many points are off and how far, you can get by with least squares regression (which is fairly easy to get working). If you have too many outliers (or they are much too large/small), you can also try robust regression (e.g. least absolute deviation fitting) using the robustfit function.
If you can manually determine the outliers, you can also fit a curve through the other points to get better results or even use interpolation methods (e.g. interp1 in MATLAB) on those points to get a smoother curve.
If you know which function describes your data, robust fitting (using, e.g. ROBUSTFIT, or the new convenient functions LINEARMODEL and NONLINEARMODEL with the robust option) is a good way to go if there are outliers in your data.
If you don't know the function that describes your data, but want a smooth trendline that is little affected by outliers, SMOOTHN from the File Exchange does an excellent job in my experience.
Have you looked at the use of smoothing splines? Like interpolating splines, but with the knot points and coefficients chosen to minimise a least-squares error function. There is an excellent implementation available from Matlab central which I have used successfully.