Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have data at the nodes of a 3D triangle, and I need to interpolate to get data inside the triangle.
here is what i tried to do:
x=[0,1,0];
y=[1,0,1];
z=[0,2,-1];
[X,Y,Z]=meshgrid(x,y,z);
v=[2,5,-1];
xs=linspace(0,1,.1);
ys=linspace(0,1,.1);
zs=linspace(-1,2,.1);
Vs = interp3(X,Y,Z,v,xs,ys,zs,'linear');
i get an error: The number of input coordinate arrays does not equal the number of dimensions (NDIMS) of these arrays.
what is wrong?
Let Xcontain the x-coordinates of your nodes, Y the y-coordinates, Z the z-coordinates of your nodes. Store the value/data at your nodes in V. Now you can specify where you want to interpolate the data by saving the x,y and z-coordinates of these points in Xs,Ys and Zs. The value of your data at these points is:
Vs = interp3(X,Y,Z,V,Xs,Ys,Zs,'linear');
You can take a look at the Matlab documentation.
Edit: As you added your code: The error seems to be that the dimension of your V is wrong. If you take a look at the example Matlab Docu -> interp3 -> Evaluate Outside the Domain of X, Y, and Z you will see, that Vhas to have the dimension as X, Yand Zcombined. From the documentation: size(V) = [length(Y) length(X) length(Z)] for vectors X ,Yand Z.
Here is an example:
X = linspace(-1,2,5);
Y = linspace(-1,7,23);
Z = linspace(3,9,23);
V = rand(23,5,23);
xq = linspace(0,1,34);
yq = linspace(0,2,34);
zq = linspace(4,5,34);
vq = interp3(X,Y,Z,V,xq,yq,zq,'linear',-1);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Please can anyone help me to solve my Matlab problem. I will share the question and it need to be solved in Matlab. Thanks in advance for your help.
Using meshgrid() to Plot Surface
This method uses two vectors to set the axes. After creating the axes the grid over coordinates to plot over (domain) is created using the meshgrid(). The meshgrid() function will take two vectors that describe the axes. After creating this grid of coordinates the function k can be evaluated for all the points on the grid-pair.
For 6 ≤ n ≤ 12 (ten values) and 0.001 ≤ p ≤ 0.009 (five values):
Here you can see the top row of images showing the vectors used to create the axes. The second row of images shows the grid formation created using meshgrid(). You can see by taking one cell/value from n and one cell value from p a coordinate pair is formed → (n,p). For example, one coordinate point can be (n,p) → (6,0.0030).
For 6 ≤ n ≤ 12 (ten values) and 0.01 ≤ p ≤ 0.1 (ten values):
Number_Of_P_Ticks = 5;
P_Axis = linspace(0.001,0.009,Number_Of_P_Ticks);
Number_Of_N_Ticks = 10;
N_Axis = linspace(6,12,Number_Of_N_Ticks);
[p,n] = meshgrid(P_Axis,N_Axis);
k = -p.*n + sqrt((p.*n).^2 + (2.*p.*n));
subplot(1,2,1); surf(p,n,k,'FaceAlpha',0.9);
title("10 Values of p and 5 Values of n");
xlabel("p"); ylabel("n");
Number_Of_P_Ticks = 10;
P_Axis = linspace(0.01,0.1,Number_Of_P_Ticks);
[p,n] = meshgrid(P_Axis,N_Axis);
k = -p.*n + sqrt((p.*n).^2 + (2.*p.*n));
subplot(1,2,2); surf(p,n,k,'FaceAlpha',0.9);
title("10 Values of p and 10 Values of n");
xlabel("p"); ylabel("n");
Ran using MATLAB R2019b
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an y*x matrix in matlab, and I want to assign a value to any point of the region of the matrix between 4 points, like A,B,C,D the image of which I know the coordinates, if the point were aligned this wouldn't be a problem, but they aren't.
Is there a function to do this ?
Edit
I have an an input matrix of numbers between 0 and 1.
The size of the matrix is 720*1280
The region is defined like this
x = [3 10 27 20 3];
y = [10 40 31 1 10];
It doesn't matter too much if the pixels are excluded or included along the edge of the rectangle, but is better if they are included.
The output matrix should be equal to the input matrix but with the values of the points inside the region of interest replaced by some other value, for example 2.
The solution I was searching for is very similar to the one that m7913d gave me.
You can use poly2mask to convert the polygon to a mask and then change the desired elements in the matrix using logical indexing as follows:
% Create a sample matrix
matrix = rand(50, 50);
% Define your region as a polygon
x = [3 10 27 20 3];
y = [10 40 31 1 10];
% Convert the polygon to a mask
mask = poly2mask(x,y,size(matrix, 1),size(matrix, 2));
% Change the elements in matrix which are inside the polygon
matrix(mask) = 123;
% Display the result
figure
imshow(matrix, 'InitialMagnification', 'fit')
hold on
plot(x, y, 'r')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a binary image of the hand like that:
I have to write a Matlab function that detects the valley between two fingers.
The parameters are the binary image and the coordinates of the two finger's tips.
I'm new in image processing and I don't know how to start.
I suggest to isolate the black area between the two input points, and then find the highest point in this connected component.
You can try the following approach (you may need to tweak some of the parameters, but it should be a good start).
I = rgb2gray(imread('<your path>'));
%input parameters - points which represents two finger tips.
x1 = 408; y1 = 441;
x2 = 454; y2 = 373;
%binarize image
I = im2bw(I);
%noise reduction - close holes
I2 = imclose(I,strel('disk',10));
%draw a line between p1 and p2
ind = drawline([y1 x1],[y2 x2],size(I));
lineMat = zeros(size(I));
lineMat(ind) = 1;
%adds the line to the image
I2 = I2 | lineMat;
%finds a point in the middle of the line
[lineY, lineX] = ind2sub(size(I),ind);
midX = lineX(ceil(length(ind)/2));
midY = lineY(ceil(length(ind)/2));
%finds a point which resides in the connected component which is between
%the line and the two finger.
xSeed = midX;
ySeed = midY -5;
%perform imfill operation, starting from (xSeed,ySeed),
%in order to find the conected component in which the point (xSeed,ySeed)
%resides.
diffMat = imfill(I2,[ySeed xSeed])~=I2;
%finding the highest point in this connected component
[Y, X] = ind2sub(size(diffMat),find(diffMat));
minInd = find(Y==min(Y),1,'first');
yValley = Y(minInd);
xValley = X(minInd);
%presents result
imshow(I);hold on;
plot(x1,y1,'r.','MarkerSize',20);
plot(x2,y2,'r.','MarkerSize',20);
plot(xValley,yValley,'b.','MarkerSize',20);
*draw line function is taken from drawline webpage.
Final result (input points in red, output point in blue).
It's just the algorithm, but all these function certainly exist in MatLab:
Compute the convex hull
Compute the difference: Convex Hull minus the original shape. Then you have all the valleys you are looking for, plus some small patterns.
Connected components labeling.
Delete the small components. Then you have all the valleys between the fingers.
Then you can select the one you want using the fingertip coordinates.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Suppose I have for vectors x1, y1, x2, y2, and I would like to plot this data (x1,y1) and (x2,y2) with different colors. The dimesions of vector x1,y1 are not the same with x2,y2.
Than I would like also to fit all this data together, with the same polynomial fit, degree 1.
Can someone help me to do it?
You can plot the vectors simply using plot:
plot(x1, y1, 'r.', x2, y2, 'b.')
where the 'r.' specifies that this first pair should be plotted in red dots, and the 'b.' specifies that the second pair should be plotted in blue dots. You can find a more complete list of color/marker options in the help documentation for plot.
To fit a polynomial to (x,y) data, you can use polyfit:
poly_coeffs = polyfit( x, y, poly_degree )
If you want to fit the same polynomial to both sets of data, you should concatenate your vectors into a single vector, e.g. (in the case of row vectors):
x = [x1, x2]
y = [y1, y2]
poly_coeffs = polyfit( x, y, poly_degree )
If you have column vectors, you would use x = [x1; x2] (note the semicolon instead of the comma) to concatenate them vertically.
And now if you wanted to plot the polynomial fit on top of the original data, you can add it on to the list of arguments to plot:
curve_x = linspace( min(x), max(x), 100 );
curve_y = polyval( poly_coeffs, curve_x );
plot(x1,y1,'r.', x2,y2,'b.', curve_x,curve_y,'k-');
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to matlab coding and I know it's simple that's why I'm trying to plot 2D datas read from a file,but I'm stuck,with my following code I'm reading the x y coordinates and trying to plot specific point with specific indexing according to given criteria ( which is p in my case I won't go too much into details), all I want to know is how can I modify the code so that I can give the correct index to the point that I want to color(i.e when the condition is satisfied plot this specific points with blue or watever),here is my code :
M=load('data1.XYZ');
x=M(:,1); %all x coordinates
y=M(:,2); %all y coordinates
xA=M(1:400,1); % x of particles A
yA=M(1:400,2); % y of particles A
xB=M(401:800,1); % x of particles B
yB=M(401:800,2); % y of particles B
Pos1=[x y]; % read in the x y coordinates
[num1,junk1] = size(Pos1);
PosA=[xA yA]; % read in the x y A coordinates
PosB=[xB yB]; % read in the x y B coordinates
[numA,junkA] = size(PosA);
[numB,junkB] = size(PosB); %no of all B particles
fprintf('Determining Distances between particles...\n');
r = zeros(numA,1);
psil_avg=0.0+0.0i;
psir_avg=0.0+0.0i;
for m=1:numA
for n=1:numA
cnt_l=0;
psi_l=0.0+0.0i;
if(m~=n)
r(m,n)=norm(PosA(m,:)-PosA(n,:));
if(r(m,n)< 1.44)
v1=PosA(m,:)-PosA(n,:);
u=[0 1];
dot=v1(:,1).*u(:,1)+v1(:,2).*u(:,2);
N=norm(v1);
cosinus=dot/N;
theta=acos(cosinus);
cnt_l=cnt_l+1;
psi_l=psi_l+(cos(theta)+6.0i*sin(theta));
psil_avg=psi_l/cnt_l;
for k=1:numA
cnt_r=0;
psi_r=0.0+0.0i;
if(m~k)
r(m,k)=norm(PosA(m,:)-PosA(k,:));
if(r(m,k)< 1.44)
v2=PosA(m,:)-PosA(k,:);
u2=[0 1];
dot2=v2(:,1).*u2(:,1)+v2(:,2).*u2(:,2);
N2=norm(v2);
cosinus2=dot2/N2;
theta2=acos(cosinus);
cnt_r=cnt_r+1;
psi_r=psi_r+(cos(theta2)+6.0i*sin(theta2));
psir_avg=psi_r/cnt_r;
p=sqrt(psi_r*psi_l);
if p > 0.94
% fprintf('bond order parameter is %f\n',p);
plot(xA(n),yA(n),'ro','Markersize',6);
hold on;
else
plot(xA(n),yA(n),'go','Markersize',8);
end
end
end
end
end
end
end
end
if anyone can help I'd be thankful
Use scatter and the following properties:
'MarkerEdgeColor' — Marker edge color
[0 0 1] (blue) (default) | 'auto' | 'none' | three-element RGB vector | string
'MarkerFaceColor' — Marker face color
'none' (default) | 'auto' | three-element RGB vector | string
Your code is a bit hard to read so I will adress this quiestion in general.
If you have groups of coordinates you would like to plot with different colors say X1,Y1 and X2,Y2 you may do the following
figure
plot(X1,Y1,'r*')
hold on
plot(X2,Y2,'b*')
hold off
This will color the first group in red with a dot and the second with blue and a dot.
The hold on, hold off is made to plot more then one plot on a single axis without clearing the previous one.
As suggested in the comments - you should probably try to avoid looping in matlab.
I could not understand your question clearly. Do you mean like this ? For example if I have two plots
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
Do you want different colours say red for 10,blue for 20, green for 30 degrees for both curves ? Is this what you want ? Please specify properly.