extract line of image (matrix) - matlab

I load images in matlab and work with them as double matrices.
Now I want to extract the data values across a straight line from one point of the image to another. This line however does not equal to a column or row (that would be easy).
How can I do that with matlab?

A line obeys the eq of the line y=a*x+b. Here x and y are coordiantes of the image. So if you want a line defined by two points (x1,y1) -> (x2,y2), the slope a is (y2-y1)/(x2-x1) and b=y1-a*x1. So next , select points in the matrix the obey the eq of the line as follows:
Create data and end points:
m=peaks(50);
x1=5 ; x2=42;
y1=21; y2=29;
calculate ew of line parameters:
a=(y2-y1)/(x2-x1);
b=y1-a*x1;
define the line:
x=x1:x2;
y=round(a*x+b);
select the proper matrix elements using linear indexing:
ind=sub2ind(size(m),y,x)
plot:
subplot(2,1,1)
imagesc(m); hold on
colormap(bone)
line([x1 x2],[y1 y2],'Color',[1 0 0]);
subplot(2,1,2)
plot(m(ind))

Related

MATLAB - scatter plot of a vector by a matrix?

I'm very new to Matlab. I'm trying to plot X, where X is an 100x1 vector, against Y, which is an 100x10 matrix. I want the result to be X vs 10 different Y values all in the same graph, different colors for each column. The only way I can think of plotting each column of this matrix is by using the hold command, but then I have to split it up so I get each column individually. Is there an easy way to do this?
Use repmat to expand X to be the same size as Y. Try plotting them with plot(X,Y) and if it looks strange, transpose each one (plot(X',Y')).
You can use linespec arguments to select linestyle, marker style, etc. For example, plot(X,Y,'.') would indicate a point at each vertex with no connecting lines.
You don't need to use repmat, just use plot instead of scatter:
plot(X,Y,'o')
Here's an example:
% some arbitrary data:
X = linspace(-2*pi,2*pi,100).'; % size(X) = 100 1
Y = bsxfun(#plus,sin(X),rand(100,10)); % size(Y) = 100 10
% you only need the next line:
plot(X,Y,'o')
legend('show')

Matlab extend plot over all axis range

I'm trying to use Matlab for some data plotting. In particular I need to plot a series of lines, some times given two points belonging to it, some times given the orthogonal vector.
I've used the following to obtain the plot of the line:
Line given two points A = [A(1), A(2)] B = [B(1), B(2)]:
plot([A(1),B(1)],[A(2),B(2)])
Line given the vector W = [W(1), W(2)]':
if( W(1) == 0 )
plot( [W(1), rand(1)] ,[W(2), W(2)])
else
plot([W(1), W(1) + (W(2)^2 / W(1))],[W(2),0])
end
where I'm calculating the intersection between the x-axis and the line using the second theorem of Euclid on the triangle rectangle formed by the vector W and the line.
My problem as you can see from the picture above is that the line will only be plotted between the two points and not on all the range of my axis.
I have 2 questions:
How can I have a line going across the whole axis range?
Is there a more easy and direct way (maybe a function?) to plot the line perpendicular to a vector? (An easier and more clean way to solve point 2 above.)
Thanks in advance.
Do you know the bounds of your axis for displaying the plot? If so, you can specify the range of the plot with the axis([xmin, xmax, ymin, ymax]) function.
So, from your question, if you know the slope m and intercept b, you can make sure your function plots the line across the whole window by specifying:
plot([xmin, xmax], [m*xmin + b, m*xmax + b]);
axis([xmin, xmax, min(m*xmin+b, m*xmax+b), max(m*xmin+b, m*xmax+b)]);
where xmin and xmax are values you specify as the range of your x-axis. This will make your line go from the corner of your plot to the other corner. If you want a buffer in the y-direction, then add one like so:
buffer = 5; % for example, you set this to something that looks good.
axis([xmin, xmax, min(m*xmin+b, m*xmax+b)-buffer, max(m*xmin+b, m*xmax+b)+buffer]);

Plot several lines in Matlab without for-loop

In Matlab I have two Nx3 matrices P and Q and each line represents a point.
I want to plot lines between the points that are in the same row of the matrices.
Following Code does it:
for i=1:N
plot3( [P(i,1) Q(i,1)], ...
[P(i,2) Q(i,2)], ...
[P(i,3) Q(i,3)] )
end
Is there a way to do it without a for-loop?
If I give plot3 the points just as vectors, Matlab draws lines between Q(i,:) and P(i+1,:) in addition to the lines I want.
Try
plot3([P(:, 1) Q(:, 1)]', ...
[P(:, 2) Q(:, 2)]', ...
[P(:, 3) Q(:, 3)]')
If you feed a matrix to the Matlab plotting functions, each column corresponds to one line drawn.
Probably you want this:
h = quiver3(P(:,1), P(:,2), P(:,3), Q(:,1), Q(:,2) , Q(:,3),0);
set(h,'ShowArrowHead','off');

Fit a line to a part of a plot

I have two array 451x1 , I want to fit a line to a part of my data, for x=3.8 –4.1 , and I want to evaluate interception of fitted line with line y=0, Do you have any idea? (In matlab )
data
You can easily perform a linear regression by indexing the points of the curve you want to use and passing them to the function POLYFIT. Here's the code to do it and a plot of the fit line:
index = (x >= 3.8) & (x <= 4.1); %# Get the index of the line segment
p = polyfit(x(index),y(index),1); %# Fit polynomial coefficients for line
yfit = p(2)+x.*p(1); %# Compute the best-fit line
plot(x,y); %# Plot the data
hold on; %# Add to the plot
plot(x,yfit,'r'); %# Plot the best-fit line
axis([1 7 0 4e10]); %# Adjust the axes limits
Then you can compute where this line intercepts the x-axis (i.e. y = 0) like so:
>> -p(2)/p(1)
ans =
3.5264
I'm just guessing that your question is to estimate a line with a zero y-intercept, although honestly, "want to evaluate interception of fitted line with line y=0" makes little sense in English to me. So this is just a complete guess, unless you choose to clarify your question.
Delete that part of the data that does NOT lie in the interval of interest. (Or if you prefer, extract only that part which does.)
Fit a line with zero y-intercept to the data of interest.
slope = x(:)\y(:);
You would fit x respect to y like:
> ndx= find(x>= 3.8& x<= 4.1);
> b= [y(ndx) ones(size(ndx))]\ x(ndx)
Now b(2) is "the interception with" line y= 0.

plot from data not a function

If you plot
sin(x*y)
you see some lines.
Now if u have all coordinates of all points of these lines and want to plot theme
(connecting dots without using sin(x*y) function), how is possible?
by this codes, i try to obtain coordinates of each 'x'(beta-bar) for each 'lam' and
save roots in a matrix.
clc; clear;
lmin=0.8; lmax=2.5;
bmin=1; bmax=1.5;
lam=linspace(lmin,lmax,100);
for n=length(lam):-1:1
increment=0.001; tolerence=1e-14; xstart=bmax-increment;
x=xstart;
dx=increment;
m=0;
while x > bmin
while dx/x >= tolerence
if sign(f(lam(n),x))*sign(f(lam(n),x-dx))<0
dx=dx/2;
else
x=x-dx;
end
end
m=m+1;
r(m,n)=x;
dx=increment;
x=0.999*x;
end
end
figure
hold on,plot(lam,r(1,:),'b')
plot(lam,r(2,:),'c')
plot(lam,r(3,:),'r')
xlim([lmin,lmax]);ylim([bmin,bmax]),
xlabel('\lambda(\mum)'),ylabel('\beta-bar')
and
function y=f(x,y)
y=sin(4*x*y);
end
what is wrong with it?
how to separately plot each line?
Use plot(X1,Y1,...,Xn,Yn)
See link for more details
http://www.mathworks.com/help/techdoc/ref/plot.html
use the plot() command. From the Matlab docu ('help plot' on the command line):
'PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
then the vector is plotted versus the rows or columns of the matrix,
whichever line up. If X is a scalar and Y is a vector, disconnected
line objects are created and plotted as discrete points vertically at
X.'
So while plot(sin(X,Y)) used the plot(X) overload of the function, you will use the plot(X,Y) overload.