I have coordinates in an matrix (an image) that I want to change, I have the coordinates in the form of a x coordinates vector and a y coordinates vector. How do I change the values of only those coordinates.
I've tried to use the notation I(x,y) but it gives me all the possible combinations:
I = zeros(10,10);
x = [4 6 8];
y = [7 3 1];
I(x,y) = 1;
imshow(I);
gives me: while what I want to get is:
using sub2ind function like the following:
I(sub2ind(size(I),x,y)) = 1
Related
I am having problem with converting Shape file which is a border of Germany with their X,Y coordinates to raster matrix format of same shape. I simply don't know which method to use for this case. I would be thankful if somebody help me to find the right way of thinking for this application.
It can be very much with ease obtained in MATLAB. YOu need to use function inpolygon. This function gives you indices of the points lying inside and putside the given polygon. Once you know the indices, you can get what you want. YOu may check the below demo:
x = [1 2 3 4 3 2]';
y = [4 5 5 4 3 3]';
k = boundary(x,y);
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
[X,Y] = meshgrid(linspace(x0,x1),linspace(y0,y1)) ;
idx = inpolygon(X(:),Y(:),x(k),y(k)) ;
X(~idx) = NaN ;
Y(~idx) = NaN ;
plot(x,y,'.r')
hold on
patch(x(k),y(k),'k') ;
plot(X(idx),Y(idx),'.r')
Just replace (x,y) above with your shape file coordinates, you will get what you want.
Let's say I have a vector of x coordinates, a matrix of y coordinates and corresponding z values:
xcoordinates = [1 2 3 4 5];
ycoordinates = repmat(xcoordinates,5,1)+rand(5,5);
z = zeros(5,5);
for x=xcoordinates
for y=1:5
z(x,y) = sqrt(x^2+ycoordinates(x,y)^2);
end
end
How do I plot a surface determined by the z values at the points given by the corresponding x and y values? The first x value defines the x value for all y values in the first row of the matrix, the second x value to all values in the second row and so on.
(If the answer is griddata I would like some additional pointers. How can I get my data into the correct format?)
mesh(repmat(xcoordinates,5,1), ycoordinates, z)
By the way, you could easily vectorize this computation:
x = repmat(1:5, 5, 1);
y = x + rand(5,5);
z = sqrt(x.^2+y.^2);
mesh(x', y, z)
I have my data in 2 vectors.
Then using [n,xout] = hist(x,y) returns vectors n and xout containing the frequency counts and the bin locations.
x are my real data and y the segment where I want to build my histogram.
Than I use bar(xout,n) to plot the histogram. In the end I am fitting this histogram with gaussian fit.
Now I would like to know where my real data (each point of vector x), are located in the histogram?
Can someone help me to figure out them?
[~, result] = min(abs(bsxfun(#minus, y(:), x(:).')));
This gives, for each value in x, the index of the closest element in y. So y(result) is the closest element in y for each x.
Example:
>> x = [0.4 1.6 5.3 4.2 3.1 7.8];
>> y = [0 2 4 6 8];
>> [~, result] = min(abs(bsxfun(#minus, y(:), x(:).')))
result =
1 2 4 3 3 5
>> y(result)
ans =
0 2 6 4 4 8
You want to use histc.
[binCounts, idx] = histc(x, y);
Then to find the bin in which a certain value of x is:
bin = idx(x == 0.4);
Just watch out since histc second input is not the centers like hist, but the end value of each bin. So you might need to change your y vector.
I'm new to Matlab but I know a bit about programming.
For class, we have been asked to generate a matrix that gives the vertices of a two dimensional n-sided shape where n>=4. Then, generate the vectors to connect the vertices. We were also given a hint: a vector for each segment can be found by adding the vectors drawn from the origin to each of two adjacent vertices.
I know how to create a matrix using A = [1 1; 1 2; 2 2; 2 1] but I'm not sure how to draw the vectors given this or any other matrix.
The plot() function looks promising, but I'm unsure how to use it with the matrix.
Thank you for any suggestions.
Btw, I'm using Matlab 2011a
I'm not exactly sure how your matrix represents your shape but you might for example let the x-coordinates of the shape be the first column of your array, then let the y-coordinates be the 2nd column, like:
A = [1 1; 1 2; 2 2; 2 1];
x = A(:,1);
y = A(:,2);
fill(x,y,'g');
axis([0 3 0 3]);
axis square;
Which in your case plots a square from the matrix A:
Or construct something a little more complicated like a pentagon:
theta = [0:pi/2.5:2*pi];
x = sin(theta);
y = cos(theta);
% your matrix is then:
B(:,1) = x;
B(:,2) = y;
B
figure;fill(x,y,'g');
axis square;
Which gives:
If you just want to plot the outline with plot (not fill the interior with fill), just remember you have to repeat the initial point at the end so that the polygonal line is closed:
A = [1 1; 1 2; 2 2; 2 1];
B = [A; A(1,:) ]; %// repeat first row at the end
plot(B(:,1),B(:,2))
axis equal %// same scale on both axes
axis([min(x)-.5 max(x)+.5 min(y)-.5 max(y)+.5]) %// larger axes for better display
I have two arrays - X points and Y points. X array have some spaces (e. g. [0 1 2 6 7 8]), and Y array contains only values for that Xes. I've got that array as a local maxima from wavelet transform. I can plot it with plot(X,Y)
Now I want to get Y's on linspace - Y must contain values for any X from 0 to 8. I want to have the same plot plot(Y) as the previous plot(X, Y).
How can I do this?
It looks like you want to perform interpolation
xPts = [0 1 2 6 7 8];
yPts = ...
xPlot = 0:1:8;
yPlot = interp1(xPts,yPts,xPlot,'cubic')
plot(xPlot,yPlot)
Check the documentation for interp1 for the different interpolation schemes.
If there are repeated x-values, you can average the corresponding y-values
xPtsRep = [0 0 1 2 6 7 7 8]
yPtsRep = ...
[xPts,~,xIdx] = unique(xPtsRep);
yPts = accumarray(xIdx,yPtsRep,[],#mean);