I have a set a section of code that is taking a long time to run. I read over the vectorization page on the mathworks site. I am still a little confused on one part, is it possible to vectorize the part where I run plane_intersect?
Unvectorized
for N = 1:sizeDimages
imPos = anaInfoSat(N).ImagePositionPatient;
A2Z = imPos(3);
A2Y = imPos(2);
A2X = imPos(1);
[upP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[uppersatX1,uppersatY1,uppersatZ1],crossS,[A2X,A2Y,A2Z]);
[loP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[lowersatX1,lowersatY1,lowersatZ1],crossS,[A2X,A2Y,A2Z]);
end
My attempt at vectorization, the thing is upP1 is a Nx3 matrix. I preallocate the upP1 matrix. This code below returns an error about dimension mismatch. ImagePosition is a 1x3 matix.
N = 1:sizeDimages;
imPos = anaInfoSat(N).ImagePositionPatient;
A2Z = imPos(3);
A2Y = imPos(2);
A2X = imPos(1);
[upP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[uppersatX1,uppersatY1,uppersatZ1],crossS,[A2X,A2Y,A2Z]);
[loP1(N,:)]=plane_intersect([cosSx(1),cosSy(1),cosSz(1)],[lowersatX1,lowersatY1,lowersatZ1],crossS,[A2X,A2Y,A2Z]);
Here is part of the plane_intersect code, should be enough to let you know what it does.
function [P,N,check]=plane_intersect(N1,A1,N2,A2)
%plane_intersect computes the intersection of two planes(if any)
% Inputs:
% N1: normal vector to Plane 1
% A1: any point that belongs to Plane 1
% N2: normal vector to Plane 2
% A2: any point that belongs to Plane 2
%
%Outputs:
% P is a point that lies on the interection straight line.
% N is the direction vector of the straight line
% check is an integer (0:Plane 1 and Plane 2 are parallel'
% 1:Plane 1 and Plane 2 coincide
% 2:Plane 1 and Plane 2 intersect)
%
% Example:
% Determine the intersection of these two planes:
% 2x - 5y + 3z = 12 and 3x + 4y - 3z = 6
% The first plane is represented by the normal vector N1=[2 -5 3]
% and any arbitrary point that lies on the plane, ex: A1=[0 0 4]
% The second plane is represented by the normal vector N2=[3 4 -3]
% and any arbitrary point that lies on the plane, ex: A2=[0 0 -2]
%[P,N,check]=plane_intersect([2 -5 3],[0 0 4],[3 4 -3],[0 0 -2]);
In your vectorized code anaInfoSat(N).ImagePositionPatient; won't return a single, but several answers. If you assign the resuts to a single variable it will receive only the first answer. This is why you get dimension mismatch error.
Depending on the data class you can combine into a matrix
imPos = [anaInfoSat(N).ImagePositionPatient];
or into a cell array
imPos = {anaInfoSat(N).ImagePositionPatient};
You can also assign to several variables as the same time:
[A2X, A2Y, A2Z] = anaInfoSat(1:3).ImagePositionPatient;
Related
I have a materials matrix where the values indicate the type of material (value between 1 and 8). Each value below 5 indicates an "interesting" material. Now at a certain point, i want to sum up the amount of non-interesting neighbor materials. So in a 3D-matrix the result at one point can be value between 0 and 6. One of the problems is that the "current" point is at the edge of the 3D matrix. I can solve this using 3 very expensive for-loops:
materials; % given 3D matrix i.e. 97*87*100
matrixSize = size(materials);
n = matrixSize(1)*matrixSize(2)*matrixSize(3); * total number of points
materialsFlattened = reshape(materials, [n 1]); % flattened materials matrix from a 3D matrix to a 1D matrix
pageSize = matrixSize(1)*matrixSize(2); % size of a page in z-direction
interestingMaterials = materialsFlattened(:) < 5; % logical vector indicating if the materials are interesting
n_bc = zeros(obj.n, 1); % amount of neighbour non-interesting materials
for l = 1:matrixSize(3) % loop over all z
for k = 1:matrixSize(2) % loop over all y
for j = 1:matrixSize(1) % loop over all x
n_bc(sub2ind(matrixSize,j,k,l)) = ...
~interestingMaterials(sub2ind(matrixSize,j,k,max(1, l-1)))...
+ ~interestingMaterials(sub2ind(matrixSize,j,max(1,k-1),l))...
+ ~interestingMaterials(sub2ind(matrixSize,max(1, j-1),k,l))...
+ ~interestingMaterials(sub2ind(matrixSize,min(matrixSize(1),j+1),k,l))...
+ ~interestingMaterials(sub2ind(matrixSize,j,min(matrixSize(2),k+1),l))...
+ ~interestingMaterials(sub2ind(matrixSize,j,k,min(matrixSize(3),l+1)));
end
end
end
So note that i first flatten the matrix to a 1D matrix using reshape. The min and max operators ensure that i do not go out of the bounds of the matrix; instead i take the value of the material where i currently am. For my application, speed is of the essence and i was hoping i can get rid of this ugly loop in loop structure. Often times that is possible in MATLAB, as the element-wise indexing is amazing and sometimes kinda magic.
I am to fit planes through various points in an image, but I am having issues with forcing the line through a particular point in the image. This happens particularly when the line is 90 degrees.
My code is as follows:
I = [3 3 3 3 3 2 2
3 3 3 3 2 2 2
3 3 3 3 2 2 2
3 3 1 2 2 2 2
1 1 1 2 2 2 2
1 1 1 1 1 2 2
1 1 1 1 1 1 1];
% force the line through point p
p = [3,3];
% points to fit plane through
edgeA = [3,3.5; 3,4; 2.5,4; 2,4; 1.5,4];
edgeB = [3.5,3; 4,3; 4.5,3; 5,3];
% fit a plane through p and edgeA
xws = [p(2), edgeA(:,2)']';
yws = [p(1), edgeA(:,1)']';
Cws = [xws ones(size(xws))];
dws = yws;
Aeqws = [p(2) 1];
beqws = [p(1)];
planefitA = lsqlin(Cws ,dws,[],[],Aeqws, beqws);
% fit a plane through p and edgeB
xwn = [p(2), edgeB(:,2)']';
ywn = [p(1), edgeB(:,1)']';
Cwn = [xwn ones(size(xwn))];
dwn = ywn;
Aeqwn = [p(2) 1];
beqwn = [p(1)];
planefitB = lsqlin(Cwn ,dwn,[],[],Aeqwn, beqwn);
%%%%% plot the fitted planes:
xAxis = linspace(0, size(I, 2), 12);
%obtain linear curve
fA = planefitA(1)*xAxis + planefitA(2);
fB = planefitB(1)*xAxis + planefitB(2);
%plot the fitted curve
RI = imref2d(size(I),[0 size(I, 2)],[0 size(I, 1)]);
figure, imshow(I, RI, [], 'InitialMagnification','fit')
grid on;
hold on;
plot(xAxis,fA, 'Color', 'b', 'linewidth', 2);
plot(xAxis,fB, 'Color', 'r', 'linewidth', 2);
All the points in edgeB fall on a 90 degrees line. However, the function ends up fitting a wrong line through those points. I know this because using
planefitB = polyfit([p(2), edgeB(:,2)'], [p(1), edgeB(:,1)'], 1);
works for this particular line but the problem is that i have these process repeated so many times at different locations in my image, hence i do not know how to suggest polyfit when the line would be 90 degrees.
Please, any ideas/suggestions on how i could make this work? Many thanks.
This amounts to the least squares solution of only the angle of the line. The offset is fixed by the fact that it has to go through (3,3). The easiest way to express this is by offsetting your data points by the known crossing. That is, subtract (3,3) from your data points, and fit the best m for y=mx, the b being fixed to 0.
For the non-vertical case, you can use a classic least-squares formulation, but don't augment the constant 1 into the Vandermonde matrix:
slope = (edgeA(:,2) - p(2)) \ (edgeA(:,1) - p(1));
This gives exactly the same answer as your constrained lsq solution.
Now for the vertical line: A non-vertical line can be expressed in the standard functional form of y=mx, where the least squares formulation implicitly assumes an independent and a dependent variable. A vertical line doesn't follow that, so the only general choice is a "Total Least Squares" formulation, where errors in both variables are considered, rather than just the residuals in the dependent (y) variable.
The simplest way to write this is to choose a and b to minimize ax - by, in the least squares sense. [x_k -y_k]*[a b].' should be as close to a zero vector as possible. This is the vector closest to the null space of the [x -y] matrix, which can be computed with the svd. Swapping columns and fudging signs lets us just use svd directly:
[u s v] = svd(bsxfun(#minus, edgeA, p));
The last column of v is the closest to the null space, so mapping back to your x/y definitions, (edgeA-p)*v(:,2) is the line, so the y multiplier is in the top position, and the x in the lower, with a sign flip. To convert to y=mx form, just divide:
slope = -v(2,2)/v(1,2);
Note that this answer will be quite a bit different than the normal least squares answer, since you are treating the residuals differently. Also, the final step of computing "slope" won't work in the vertical case for the reasons we've already discussed (it produces Inf), so you are probably better off leaving the line as a normalized 2-vector, which won't have any corner cases.
I want to write a script to generate a 3D vector field of the electric flux density of 8 different point charges in a [-2,2]x[-2,2]x[-2,2] box in 3D space.
I have a function definition in a separate .m file as follows:
function[Dx,Dy,Dz]= question3function(Q,Loc,XX,YY,ZZ)
Q=1e-6;
Loc=[];
XX=(2,-2);
YY=[2,-2];
ZZ=[2,-2];
% Position vector from the point charge
Rx=(XX)-Loc([]);
Ry=(YY)-Loc([]);
Rz=(ZZ)-Loc([]);
% Distance between position in interest and the point charge
R=sqrt(Rx.*Rx+Ry.*Ry+Rz.*Rz);
% Unit Position vector
Ax=Rx./R;
Ay=Ry./R;
Az=Rz./R;
% Electric flux density XYZ components
K=Q./(4*pi*R.^2);
Dx=K.*Ax;
Dy=K.*Ay;
Dz=K.*Az;
And then in my main script I have the function calls:
%function calls
[Dx1,Dy1,Dz1]=question3function(Q,[1 1 1],XX,YY,ZZ);
[Dx2,Dy2,Dz2]=question3function(Q,[1 1 -1],XX,YY,ZZ);
[Dx3,Dy3,Dz3]=question3function(Q,[1 -1 1],XX,YY,ZZ);
[Dx4,Dy4,Dz4]=question3function(-Q,[1 -1 -1],XX,YY,ZZ);
[Dx5,Dy5,Dz5]=question3function(2*Q,[-1 1 1],XX,YY,ZZ);
[Dx6,Dy6,Dz6]=question3function(-2*Q,[-1 1 -1],XX,YY,ZZ);
[Dx7,Dy7,Dz7]=question3function(-Q,[-1 -1 1],XX,YY,ZZ);
[Dx8,Dy8,Dz8]=question3function(-Q,[-1 -1 1],XX,YY,ZZ);
Dx=Dx1+Dx2+Dx3+Dx4+Dx5+Dx6+Dx7+Dx8;
Dy=Dy1+Dy2+Dy3+Dy4+Dy5+Dy6+Dy7+Dy8;
Dz=Dz1+Dz2+Dz3+Dz4+Dz5+Dz6+Dz7+Dz8;
quiver3(XX,YY,ZZ,Dx,Dy,Dz);
axis square equal;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Electric Flux Density of the sum of 8 Point Charges');
I receive the following errors when I try to run my function file:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> question3function at 11
Rx=(XX)-Loc([]);
Could somebody please help me and explain how I can fix this? I will add I am not very experienced with using MATLAB.
There are a number of meaningless things that you are doing here:
function[Dx,Dy,Dz]= question3function(Q,Loc,XX,YY,ZZ)
Q=1e-6; % this is meaningless
Loc=[]; % this is meaningless
XX=(2,-2); % this is meaningless ()
YY=[2,-2]; % this is meaningless
ZZ=[2,-2]; % this is meaningless
% Position vector from the point charge
Rx=(XX)-Loc([]); % this is meaningless ([])
Ry=(YY)-Loc([]); % this is meaningless ([])
Rz=(ZZ)-Loc([]); % this is meaningless ([])
% Distance between position in interest and the point charge
R=sqrt(Rx.*Rx+Ry.*Ry+Rz.*Rz);
% Unit Position vector
Ax=Rx./R;
Ay=Ry./R;
Az=Rz./R;
% above why do you need all of them to be unit vectors
% you can
% Electric flux density XYZ components
K=Q./(4*pi*R.^2);
Dx=K.*Ax;
Dy=K.*Ay;
Dz=K.*Az;
Moreover, in %function calls how are defining XX, YY and ZZ?
The second half of your function declaration is sound, but you're missing the point of how functions work. The point of function parameters is that the function uses those variables as input. Assigning to input parameters is usually a semantic error: it's generally not what you want to do. Another problem is that in matlab vectors are defined with square brackets, and frankly (2,-2) should give you an error about unbalanced parentheses... Always make sure to check your code before posting a question about it on Stack Overflow: if you change it before posting, you might have inadvertently removed your original problem.
You can use your original function by removing the superfluous variable redefinitions at the beginning, and by fixing the assignment to Rx/Ry/Rz. When you say Loc([]), you are indexing with an empty vector, and the result is an empty variable. This is obviously not what you need. Instead:
function [Dx,Dy,Dz]=question3function(Q,Loc,XX,YY,ZZ)
%Q, Loc, XX, ZZ, YY: input!
% Position vector from the point charge
Rx = XX - Loc(1); %use first component of Loc for every x
Ry = YY - Loc(2); %use second component of Loc for every y
Rz = ZZ - Loc(3); %use third component of Loc for every z
% Distance between position in interest and the point charge
R=sqrt(Rx.^2+Ry.^2+Rz.^2); % .^2 takes less characters
% Unit Position vector
Ax=Rx./R;
Ay=Ry./R;
Az=Rz./R;
% Electric flux density XYZ components
K=Q./(4*pi*R.^2);
Dx=K.*Ax;
Dy=K.*Ay;
Dz=K.*Az;
Then you should define XX, YY, ZZ, Q and Loc in your calling function/script. Also, if you're only interested in the total flux density, you could spare some keyboard-time by defining every charge and their location in arrays, and calling your function in a loop:
%define mesh
N = 5; % number of points for mesh in each dimension
[XX,YY,ZZ] = meshgrid(linspace(-2,2,N));
%define charges and locations in arrays
Q0 = 1e-6;
Qvec = Q0*[1 1 1 -1 2 -2 -1 -1];
Locmat = [1 1 1; 1 1 -1; 1 -1 1; 1 -1 -1;...
-1 1 1; -1 1 -1; -1 -1 1; -1 -1 1]; % last 2 are duplicates!!
%function calls in loop, keep adding up flux components
Dx=zeros(size(XX));
Dy=zeros(size(XX));
Dz=zeros(size(XX));
for k=1:length(Qvec)
Q = Qvec(k);
Loc = Locmat(k,:);
[Dxtmp, Dytmp, Dztmp] = question3function(Q,Loc,XX,YY,ZZ);
Dx = Dx + Dxtmp;
Dy = Dy + Dytmp;
Dz = Dz + Dztmp;
end
%plot, no changes here
quiver3(XX,YY,ZZ,Dx,Dy,Dz);
axis square equal;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Electric Flux Density of the sum of 8 Point Charges');
Output:
I have two datasets, one of which is a target position, and the other is the actual position. I would like to plot the target with a +/- acceptable range and then overlay with the actual. This question is only concerning the target position however.
I have unsuccessfully attempted the built in area, fill, and rectangle functions. Using code found on stackoverflow here, it is only correct in certain areas.
For example
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1; %variation in target size
y2 = y-1;
t = 1:15;
X=[t,fliplr(t)]; %create continuous x value array for plotting
Y=[y1,fliplr(y2)]; %create y values for out and then back
fill(X,Y,'b');
The figure produced looks like this:
I would prefer it to be filled within the red boxes drawn on here:
Thank you!
If you would just plot a function y against x, then you could use a stairs plot. Luckily for us, you can use the stairs function like:
[xs,ys] = stairs(x,y);
to create the vectors xs, ys which generate a stairs-plot when using the plot function. We can now use these vectors to generate the correct X and Y vectors for the fill function. Note that stairs generates column vectors, so we have to transpose them first:
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1]; % Target datum
y1 = y+1; %variation in target size
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
X=[ts.',fliplr(ts.')]; %create continuous x value array for plotting
Y=[ys1.',fliplr(ys2.')]; %create y values for out and then back
fill(X,Y,'b');
Again, thank you hbaderts. You answered my question perfectly, however when I applied it to the large data set I needed for, I obtained this image
https://dl.dropboxusercontent.com/u/37982601/stair%20fill.png
I think it is because the fill function connects vertices to fill?
In any case, for the potential solution of another individual, combined your suggested code with the stair function and used the area function.
By plotting them on top of one another and setting the color of the lower area to be white, it appears as the rectangular figures I was after.
%sample code. produces image similar to o.p.
y = [1 1 1 2 1 1 3 3 1 1 1 1 1 1 1];
y1 = y+1;
y2 = y-1;
t = 1:15;
[ts,ys1] = stairs(t,y1);
[ts,ys2] = stairs(t,y2);
area(ts,ys1,'FaceColor','b','EdgeColor','none')
hold on
area(ts,ys2,'FaceColor','w','EdgeColor','none')
https://dl.dropboxusercontent.com/u/37982601/stair%20area.png
Thanks again for your help and for pointing me in the right direction!
Background:
Basically I'm using a dynamic time warping algorithm like used in speech recognition to try to warp geological data (filter out noise from environmental conditions) The main difference between these two problems is that dtw prints a warping function that allows both vectors that are input to be warped, whereas for the problem I'm trying to solve I need to keep one reference vector constant while stretching and shrinking the test variable vector to fit.
here is dtw in matlab:
function [Dist,D,k,w]=dtw()
%Dynamic Time Warping Algorithm
%Dist is unnormalized distance between t and r
%D is the accumulated distance matrix
%k is the normalizing factor
%w is the optimal path
%t is the vector you are testing against
%r is the vector you are testing
[t,r,x1,x2]=randomtestdata();
[rows,N]=size(t);
[rows,M]=size(r);
%for n=1:N
% for m=1:M
% d(n,m)=(t(n)-r(m))^2;
% end
%end
d=(repmat(t(:),1,M)-repmat(r(:)',N,1)).^2; %this replaces the nested for loops from above Thanks Georg Schmitz
D=zeros(size(d));
D(1,1)=d(1,1);
for n=2:N
D(n,1)=d(n,1)+D(n-1,1);
end
for m=2:M
D(1,m)=d(1,m)+D(1,m-1);
end
for n=2:N
for m=2:M
D(n,m)=d(n,m)+min([D(n-1,m),D(n-1,m-1),D(n,m-1)]);
end
end
Dist=D(N,M);
n=N;
m=M;
k=1;
w=[];
w(1,:)=[N,M];
while ((n+m)~=2)
if (n-1)==0
m=m-1;
elseif (m-1)==0
n=n-1;
else
[values,number]=min([D(n-1,m),D(n,m-1),D(n-1,m-1)]);
switch number
case 1
n=n-1;
case 2
m=m-1;
case 3
n=n-1;
m=m-1;
end
end
k=k+1;
w=cat(1,w,[n,m]);
end
w=flipud(w)
%w is a matrix that looks like this:
% 1 1
% 1 2
% 2 2
% 3 3
% 3 4
% 3 5
% 4 5
% 5 6
% 6 6
so what this is saying is that the both the first and second points of the second vector should be mapped to the first point of the first vector. i.e. 1 1
1 2
and that the fifth and sixth points on the first vector should be mapped to the second vector at point six. etc. so w contains the x coordinates of the warped data.
Normally I would be able to say
X1=w(:,1);
X2=w(:,2);
for i=1:numel(reference vector)
Y1(i)=reference vector(X1(i));
Y2(i)=test vector(X2(i));
end
but I need not to stretch the reference vector so I need to use the repeats in X1 to know how to shrink Y2 and the repeats in X2 to know how to stretch Y2 rather than using repeats in X1 to stretch Y1 and repeats in X2 to stretch Y2.
I tried using a find method to find the repeats in both X1 and X2 and then average(shrink) or interpolate linearly(stretch) as needed but the code became very complicated and difficult to debug.
Was this really unclear? I had a hard time explaining this problem, but I just need to know how to take w and create a Y2 that is stretched and shrunk accordingly.
First, here's DTW in Matlab translated from the pseudocode on wikipedia:
t = 0:.1:2*pi;
x0 = sin(t) + rand(size(t)) * .1;
x1 = sin(.9*t) + rand(size(t)) * .1;
figure
plot(t, x0, t, x1);
hold on
DTW = zeros(length(x0), length(x1));
DTW(1,:) = inf;
DTW(:,1) = inf;
DTW(1,1) = 0;
for i0 = 2:length(x0)
for i1 = 2:length(x1)
cost = abs(x0(i0) - x1(i1));
DTW(i0, i1) = cost + min( [DTW(i0-1, i1) DTW(i0, i1-1) DTW(i0-1, i1-1)] );
end
end
Whether you are warping x_0 onto x_1, x_1 onto x_0, or warping them onto each other, you can get your answer out of the matrix DTW. In your case:
[cost, path] = min(DTW, [], 2);
plot(t, x1(path));
legend({'x_0', 'x_1', 'x_1 warped to x_0'});
I don't have an answer but I have been playing with the code of #tokkot implemented from the pseudocode in the Wikipedia article. It works, but I think it lacks three requeriments of DTW:
The first and last points of both sequences must be a match, with the use of min(), some (or many) of the first and ending points of one of the sequences are lost.
The output sequence is not monotonically increasing. I have used x1(sort(path)) instead, but I don't believe it is the real minimum distance.
Additionally, for a reason I haven't found yet, some intermediate points of the warped sequences are lost, which I believe is not compatible with DTW.
I'm still searching for an algorithm like DTW in which one of the sequences is fixed (not warped). I need to compare a time series of equally spaced temperature measurements with another sequence. The first one cannot be time shifted, it does not make sense.