Matlab ploting an arrow between two points (not just a line) [duplicate] - matlab

This question already has answers here:
How do I display an arrow positioned at a specific angle in MATLAB?
(3 answers)
How to add arrows to line plots in Matlab?
(5 answers)
How to draw an arrow in Matlab?
(5 answers)
Closed 5 years ago.
Following up on this lovely answer https://stackoverflow.com/a/45442483/2798895 how to draw a line between two sets of points I need to have an arrow (--->) between the corresponding points. Is there a way of doint it?
I found some examples of using quiver but they don't connect from (x,y) to (u,v). They are adding only a short arrow to (x,y) that is pointing in the direction of (u,v) but not touching it.
My line plot (from the above mentioned answer):
set1 = [1,2; 3,4; 5,6];
set2 = [10,20; 30,40; 50,60];
figure;
plot(set1(:,1),set1(:,2),'b+',set2(:,1),set2(:,2),'g+')
hold on
x = [set1(:,1) set2(:,1)].';
y = [set1(:,2) set2(:,2)].';
plot(x,y,'r')
hold off
drawnow
My quiver plot:
set1 = [1,2; 3,4; 5,6];
set2 = [10,20; 30,40; 50,60];
figure;
plot(set1(:,1),set1(:,2),'b+',set2(:,1),set2(:,2),'g+')
hold on
quiver(set1(:,1),set1(:,2),set2(:,1),set2(:,2));
hold off
drawnow
How would that be possible to connect the points with an arrow e.g. like quiver but with longer arrows?

Related

Unexpected output of plot [duplicate]

This question already has answers here:
What is the advantage of linspace over the colon ":" operator?
(3 answers)
Closed 4 years ago.
I am wanting to do a very simple plot of a polynomial using the plot(x,y) function. Here is my full code
a = linspace(-3, 0.1, 3);
plot(a, a.^3 - 3*a - 2);
ax = gca;
c = ax.Color;
grid on;
Which outputs the following picture
Why doesn't the graph extend from -3 to 3 on the x-axis? Why does it stop just after $0$?
As the documentation states, in linspace(x1,x2,n) x1 is the begin value, x2 is the end value and n is the number of points. That's exacly what you see on your plot: 3 points: -3, 0.1 and one halfway (because of the linear spacing).
Since you want to have a particular spacing between your points, and not a certain number of points, I suggest you build your vector as:
a = -3:.1:3;

MATLAB - How to measure mean intensity of a circular area in an image? [duplicate]

This question already has answers here:
mean value in a sphere
(3 answers)
Closed 5 years ago.
I want the mean intensity of an image, not the whole but only in a certain region of interest. This happens to be circular in shape and I know details of its radius and position.
Just inside the circular ROI (circle not drawn, using the radius and position). I dont want to mask the image and delete the outsides because that'll add unnecessary datapoints to the average.
You can use Boolean indexing
% creating the image:
[xx, yy] = meshgrid(1:100);
r = 23; x0 = 45; y0 = 67;
pixelsInSphere = ((xx-x0).^2 + (yy-y0).^2 < r);
im = pixelsInSphere.*(128+10*randn(size(yy)));
figure; imagesc(im);
% calculating the mean of the pixels using boolean indexing:
m = mean(im(pixelsInSphere));
You can create an Indicator function which given a position of pixel (i, j) it will say whether it is inside the ROI or not.
Loop over all the pixel in the image.
Sum the pixels which are in the ROI according to the indicator.

Remove overlapped Circles in MATLAB [duplicate]

This question already has answers here:
Non overlapping randomly located circles
(4 answers)
Closed 5 years ago.
I have written a MATLAB code to be able to visualize some Circles. Please have a look at my below code and the attached figure as the output.
clc;
clear;
close all;
% X and Y of each Center
Xloc = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5];
Yloc = [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5];
% Radius of each circle
radius = unifrnd(0,1,[1 numel(Xloc)]);
% Random colours
allColours = rand(numel(Xloc),3);
% Transform the data into position = [left bottom width height]
pos = [Xloc(:)-radius(:) Yloc(:)-radius(:) 2*radius(:)*[1 1]];
% Create and format the axes
H = axes;
hold on;
axis equal;
box on;
set(H,'XTickLabel',[],'YTickLabel',[]);
% Create the circles
for idx = 1:numel(Xloc);
rectangle(...
'Position',pos(idx,:),...
'Curvature',[1 1],...
'FaceColor',allColours(idx,:),...
'EdgeColor','none');
end
The output figure is (Circles' radius is generated randomly, so if you execute the code, you will face with a new output):
As you can see in the figure, there is overlap between circles. I was wondering how can I separate centers from each other to do not overlap each others, and at the same time they keep the original distance (or similar distance) from each other in [Xloc Yloc]
You can try to formulate an optimisation problem:
You have to constrain the minimal distance between the centers based on the desired radii.
You have to minimise the deviations from its desired center location.

Shade area between two vertical lines [duplicate]

This question already has answers here:
MATLAB, Filling in the area between two sets of data, lines in one figure
(4 answers)
Closed 6 years ago.
I have data for 12 time points y1=[.61 .52 .45 .75 .76 .79 .82 .6 .66 .54 .43 .21]; I would like to plot this as a line plot and draw two vertical line at time point 7 and 8 and shade the area in between these two lines. This shaded area should be transparent enough to still show the line. I would also like to have a legend to show that shaded area = critical period or have "critical period" written within the area underneath the line. From this answer, I've tried:
y1=[.61 .52 .45 .75 .76 .79 .82 .6 .66 .54 .43 .21];
N=size(y1,2);
sky_blue = [0, 0, 1] ;
x=1:N;
plot([1:N]', y1, 'linewidth', 2.8, 'linestyle', '-', 'color', sky_blue);
hold on
x1=7;
x2=8;
y2=get(gca,'ylim');
plot([x1 x1],y2)
plot([x2 x2],y2)
h1 = fill(x1,x2,'b','EdgeColor','none');
The issue is that things are okay until the last line with which I can't get the shading between the two lines. Can anyone help?
You got it almost right! Simply change your last line of code to the following:
h1 = fill([x1 x1 x2 x2], [y2 fliplr(y2)], 'b','EdgeColor','none');
fill() function takes as input the x and y coordinates of the corners (vertices) of the region (polygon) to fill the color into in either the clockwise or the anti-clockwise fashion (polygon need not be closed (fill can close it for you)).
Here, we have passed in the vector arrays of the x and y coordinates of the four vertices of the polygon bounded by the two lines in the clockwise order starting from bottom left vertex. Note: fliplr() function just reverses the 1x2 column vector, y2 from left to right.

Plot region of the inequalities using matlab [duplicate]

This question already has answers here:
How to plot inequalities
(3 answers)
Closed 8 years ago.
I have a question about matlab. It seems simple but I can't find the solution for inequalities linear region plot in matlab. For example, I want to plot the regions of y-x, and y>x and show the color for each one. For any x, y, but we can assume x = [-50:50].
Thank you.
I tried this one but don't know how to show the color for the third parameter.
[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1);
ineq1 = Y<X;
ineq2 = Y>X;
ineq3 = Y>-X;
colors = zeros(size(X))+ineq1+ineq2+ineq3;
scatter(X(:),Y(:),3,colors(:),'filled')
[X,Y]=meshgrid(-1:0.01:1,-1:0.01:1);
x1=reshape(X,[size(X,1)*size(X,2) 1]);
y1=reshape(Y,[length(x1) 1]);
a=[x1 y1];
ineq1=a(a(:,1)>a(:,2),:);
ineq2=a(a(:,1)<a(:,2),:);
ineq3=a(-a(:,1)<a(:,2),:);
scatter(ineq1(:,1),ineq1(:,2),3,'b','filled');
hold on;
scatter(ineq2(:,1),ineq2(:,2),3,'r','filled');
scatter(ineq3(:,1),ineq3(:,2),3,'g','filled');