How to draw a 3D surface plot with numerical datas on matlab - matlab

I want to draw a surface on matlab with numerical datas like :
f(x,y,z) = w
f(1,3,5) = 12
f(2,4,6)= 3
f(3,8,12)= 2
f(2,13,22)= 1
and etc...
i found plot::matrixplot on mupad but it has two problems :
first it's on mupad but i want something that do the same on script environment
second it draw only these types of data : f(x,y)=z
sample of mupad :
A := [[2, 1, 1],
[3, 4, 3],
[3, 5, 4],
[2, 6, 5]]:
plot(plot::Matrixplot(A))
sample of plot::matrixplot

Related

Find if coordinates form a complete path

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.

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.

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

how to calculate PSD of signal with various distance of time in matlab

I have a signal x(t) with it's value just like:
t = 0, 1, 3, 4, 7, 8, 10, 15, ...
x = 3, 4, 5, 4, 6, 7, 4, 8, ...
as you can see, the problem herre is the time distance between 2 any sample which's not equal. can you tell me how to use matlab to plot it's psd ?
Thank a lot.
First I would resample the data with interpolation to have a regularly sampled signal. For interpolation algorithm I use spline but you can use also interp1 which would be faster;
t2 = t(1):step:t(end); %with this I have a t signal with every sample separated by step
x2 = spline(t, x, t2);
Then you can PSD your x2 signal with fft, pwelch...

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'})