Find if coordinates form a complete path - matlab

So I have a list of points that generally form a sort of circular shape except there are often little offshoots from the circle that are essentially just lines from say the border of the circle going in a certain direction. I want to create a function that when given this list of coordinates/points finds whether there exists a complete path in this set of points.
I've thought about creating a start point and finding whether there exists a path that doesn't repeat points (ie (1,1) -> (2, 1) -> (1,1) disallowed) and could get back to the start point; however, if the start point is in an offshoot of the circle, this wouldn't work.
For instance, a list of coordinates
[[0, 0], [0, 1], [1, 2], [2, 3], [3, 3], [3, 4], [4, 4], [3, 2], [3, 1], [3, 0], [2, -1], [1, -1], [0, -1]]
would form a complete path while if I take out [1, -1] it would not form a complete path.

The solution I went with is to turn the list of neighbors into a matrix, turn that matrix into a matrix of logicals, and then assuming you know for sure a point within the loop, use imfill function on that point and check if the [1,1] coordinate had been converted.
mat = zeros(length, length);
mat(coordinates) = 1;
mat = logical(mat);
mat = imfill(mat, [length/2 length/2]);
if mat(1) == 1
not closed loop
else
closed loop
end
Several assumptions with this code are that the middle point lies within the loop and not already filled and the (1,1) coordinate value will not already be filled which are assumptions I can make with the data I am working with.

Related

Matlab Plotting Two Matrixes and Marking some X-Coordinates On it Based off another vector

Say I have two vectors I want to plot on matlab, and I have this vector that I want to use to mark a small "X" on the plot where this X-value occurs on one of the vectors, how do I do that?
To clarify, say I have a vector of a = [1, 2, 3, 4, 5] another of b = [1, 2, 3, 4, 5, 6] and an identifier vector of a = [1, 4] how do I plot these and show an X on a/b on the plot on x=1 and x =4?
Actually, to find the points that you want, you can use the ismember function as show below.
a=1:5;
c=[1 4];
hold on
plot(a(~ismember(a,c)),'ro') %values of a that DO NOT match the extra entry
plot(a(ismember(a,c)),'rx') %values of a that match the extra entry
I'm not 100% clear if it is this what you want. You can give some comments and I (or someone else) can give you a better answer.

Residual Neural Network: Concatenation or Element Addition?

With the residual block in residual neural networks, is the addition at the end of the block true element addition or is it concatenation?
For example, would addition([1, 2], [3, 4]) produce [1, 2, 3, 4] or [4, 6] ?
It would result in [4, 6], and you can find out more in this paper
The operation F + x is performed by a shortcut
connection and element-wise addition
It is from the popular ResNet paper by Microsoft Research. Therefore it is element-wise addition, hence [4, 6]

How to interpolate this?

I have a problem with interpolating.
the plot of the points is
here.
If we zoom in , the plot looks something like this
this.
I do not know how to use the Matlab interp1 function to interpolate this.
Explanation:
This is a space time diagram, i.e. the x axis is the space and the y-axis is the time.
Hence, the structure of the vectors x and y is as follows:
The vector y is defined as
y=(1,1,2,2,3,3,4,4,...,2500,2500)
and the x-vector contains (pairwise) the positions, i.e.
x(i) and x(i+1) are the different positions at time y(i)=y(i+1).
I think the problem maybe is that at different times, we have the same points as can be seen in the zoomed in picture above.
How many time-steps the x-values remain the same differs, sometimes they remain the same for 3 time-steps, sometimes even for 4 time-steps, sometimes only for 2 time-steps.
You can use acccumarray to perform some operation on the values in Y that correspond to each unique value of X.
Using some example data:
x = [1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 8];
y = 1:length(x);
We get something like the following:
Now use accumarray to perform some calculation on each cluster:
clusteravg = accumarray(x', y, [], #mean);
clustermax = accumarray(x', y, [], #max);
clustermin = accumarray(x', y, [], #min);
The first input to accumarray is an array of subscripts, the second input is an array of values that correspond to those subscripts. accumarray collects all of the elements of the second input that share the same subscript (first input) and performs the specified calculation on them. Here I've calculated the average, max, and min for each cluster of values:
Yay

Matlab 3D plot plotting weird lines?

I have 3 arrays X, Y, Z that look something like this:
x = [1, 1, 1, 2, 2, 2, 3, 3, 3]
y = [1, 2, 3, 1, 2, 3, 1, 2, 3]
z = [1, 2, 3, 2, 4, 6, 3, 6, 9]
Then I am plotting these points with plot3(x,y,z).
However the result is something unexpected.. It's basically meant to appear like a log-graph, it does this but it also has extra lines. I have no idea how it's happening, maybe someone here can enlighten me!
Here is an example of my graph (obviously I've plotted my values for X, Y, Z and the arrays above are just an example of what they look like):
As you can see, the bottom curved lines are the log-graph lines which I am happy to be seeing, but the one that appears above it confuse me. I tried displaying the X Y Z values that match the following pattern: X is between 110 and 120 and Y = 0.05. This should theoretically give me TWO matches for each X. ie. Between X = [110, 120], I should get 20 matches since the graph shows two lines hitting at the point Y = 0.05 for each X.
To make it more clear what values of X are being graphed, it's basically increments of 0.1 so between X = 120 and X = 119, it is plotting 119.0, 119.1, ... , 120.0. In any case, only two points of X, Z hit Y = 0.05 between 119-120.
As I was saying, I checked for the values of X, Z that matched Y = 0.05 and it only returns ONE result per X. That is, for X = [110, 120], only 10 matches returned. These values were values on the bottom lines (ie. the log-graph lines) and NOT the top line. So effectively, these lines are not meant to exist. They are definitely hitting Y = 0.05 and are between X = [110, 120] so they should be appearing as a match..
So there you have it. Not really sure what's going on!
If someone could help, that'd be great.
EDIT - More Info.
Even manually putting the X, Y values to the function which returns Z never reaches the values on the upper line.
Thanks.
The example for x, y and z actually illustrates your problem quite well.
You are not plotting individual curves (one for each log-plot) but rather one continuous curve. Hence your extra lines are in fact connecting one end of a log curve to the beginning of the next.
You have to break up your plot command.
Edit
In the end it will require knowledge of your dataset, but for the x, y, z you provided above, this will work to produce three independent curves instead of one:
x = [1, 1, 1, 2, 2, 2, 3, 3, 3];
y = [1, 2, 3, 1, 2, 3, 1, 2, 3];
z = [1, 2, 3, 2, 4, 6, 3, 6, 9];
DATASET_SIZE = 3;
hold on;
for i=1:size(x,2)/DATASET_SIZE
plot3(x((i-1)*DATASET_SIZE+1:i*DATASET_SIZE),y((i-1)*DATASET_SIZE+1:i*DATASET_SIZE),z((i-1)*DATASET_SIZE+1:i*DATASET_SIZE));
plot3(x(i*DATASET_SIZE+1:2*DATASET_SIZE),y(i*DATASET_SIZE+1:2*DATASET_SIZE),z(i*DATASET_SIZE+1:2*DATASET_SIZE));
plot3(x(2*DATASET_SIZE+1:3*DATASET_SIZE),y(2*DATASET_SIZE+1:3*DATASET_SIZE),z(2*DATASET_SIZE+1:3*DATASET_SIZE));
view(3)
end
Edit 2
An easy way of finding out if this "problem" occured, is using the Data Cursor in the MATLAB plot window. Place it anywhere on your graph and use the up or down arrow keys to move around. If you can move along the entire length of the plot, it is a continuous line. Furthermore, you should see the Cursor jump from the "end" of one curve to the "start" of the next.
I had the same problem using surface(x,y,z) and addressed the issue by changing the renderer. MATLAB has 3 different renderers for plotting data and by default the openGL renderer plots as you have shown. You can view hwich renderer your figure uses with GET(gcf). You can change renderer as I have done using: set(gcf,'Renderer','painters')
The 3rd renderer I believe is zbuffer and may work as well.

Repeating sequences in a single matlab plot

Say I have 2 vectors:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
b = [1, 3, 5, 7, 9, 7, 5, 3, 1];
I want to plot these vectors against each other, a being X, and b being Y.
However, while I want the plotted point to be in the correct location, I want the actual values shown on the X axis to be a function of the values in a, where the result of the function is repeated over a given period AND I want these values to be sequential along the axis.
For example, say the function for the value to show on the X axis is mod(a - 1, 3) + 1. I would like for the X axis to read something like 1, 2, 3, 1, 2, 3, 1, 2, 3 as in
I have a feeling that some combination of subplot and axes may be required, but I'm not seeing anything obvious in the documentation.
You can set this via the axes properties xtick and xticklabel.
set(gca, 'xtick', a, 'xticklabel', mod(a,3)+1);
This won't give you the ticks you've described, but mod(a-1,3)+1 will. I'm not sure if you wanted the function you gave or the picture you showed, though.
Try setting the XtickLabels, like so,
>> set(gca,'XTickLabel',{'1','2','3'})