I have a 3D grid set up as follows: [X,Y,Z] = ndgrid(0:2^(-8):1). I would like to evaluate and perform operations with vector fields defined at each point in a MATLAB meshgrid/ ndgrid. In other words, I want to assign three-dimensional vectors to every point in a mesh and perform pointwise operations with these vectors.
Implementations such as vec = [X.^2 ; 2*Y] end up concatenating matrices which makes it hard to perform operations such as dot and cross products.
Is there a way I can define a vector-valued function at each point in a mesh which takes as input, the coordinates of that point (the documentation only talked about evaluation scalar functions on meshes)? Or maybe there is a simpler implementation inbuilt in MATLAB?
Honestly, anything that would allow me to avoid subbing at 2^24 points would be appreciated.
Thanks!
Edit
My current implementation using symbolic substitution is as follows:
n=256;
[X,Y,Z] = ndgrid(0:1/n:1); % declaring 3D grid.
syms x y z; % declaring position variables.
xx = [x,y,z];
r1 = norm(xx-c1); % scalar field over grid.
n1 = (xx-c1)/r1; % vector field over grid.
v1 = cross(s1,n1); % vector field over grid.
w1 = n1'*v1; % tensor field over grid.
a = dot(v1,w1); % scalar field over grid.
% subbing in the value of scalar field 'a' (symbolic) into 3D array of doubles, A.
A = zeros(n+1,n+1,n+1);
for c = 1:n+1 % X
for d=1:n+1 % Y
for e= 1:n+1 % Z
A(c,d,e) = subs(a,[x,y,z],[X(c,0,0),Y(0,d,0),Z(0,0,e)]);
end
end
end
% use A in future computations.
How to automatically select the column vector of a matrix within which the scalar values in a subset of elements is closest to those in a predefined goal vector of the same sub set?
I solved the problem and tested method on 100,10 matrix, it works - should also work for larger matrices, while hopefully not becoming too computationally expensive
%% Selection of optimal source function
% Now need to select the best source function in the data matrix
% k = 1,2,...n within which scalar values of a random set of elements are
% closest to a pre-defined goal vector with the same random set
% Proposed Method:
% Project the columns of the data matrix onto the goal vector
% Calculate the projection error vector matrix; the null space of the
% local goal vector, is orthogonal to its row space
% The column holding the minimum error vector is the optimal column
% [1] find the null space of the goal vector, containing the projection
% errors
mpg = pinv(gloc);
xstar = mpg*A;
p = gloc*xstar;
nA = A-p;
% [2] the minimum error vector will correspond to the optimal source
% function
normnA = zeros(1,n);
for i = 1:n
normnA(i) = norm(nA(:,i));
end
minnA = min(normnA);
[row,k] = find(normnA == minnA);
disp('The optimal source function is: ')
disp(k)
I'm looping over a vector and calling a function for every element in it
I want to get rid of the loop, I can't see how.
a short description about the code:
I have imageA and ImageB, I want to map ImageB into ImageA where the user chooses 4 points in ImageA that locates the position of the pattern (ImageB)
this is the part of code that has the for loop
newIm=im1; % newIm will have the mapped image
[r,c]=size(im1); %size of imageA
[p1,p2]=meshgrid(1:c, 1:r); % using indexes matrixes
% p1 has the X indexes
% p2 has the Y indexes
z = ones(r,c); % a vector of ones (for mapping algorithm)
% now calculate for every point in imageA the indexes for its new value
% from imageB ( Inverse Mapping)
% X a b e X'
% Y = pinv c d f * Y'
% 1 g h 1 1
newmatrix=pinv(mat)*[p1(:) p2(:) z(:)]';
newX=newmatrix(1,:)./newmatrix(3,:); % devide the x with z
newY=newmatrix(2,:)./newmatrix(3,:); % devide the y with z
newX=round(newX);
newY=round(newY);
% %% the indexes we want are the ones in the range of ImageB
Xindex=(newX<Br & newX>1); % this gives us a vector of zeros and ones
Yindex=(newY<Bc & newY>1);
ourPixels=Xindex(:).*Yindex(:); % gives us a vectors that has ones
% in the places where the Y and X are both in the range
ourIndexes=find(ourPixels==1); % a vector that holds the indexes that we want
IndexesSize=size(ourIndexes);
XXX=newX(ourIndexes); %the indexes we want
YYY=newY(ourIndexes);
% loop the quadrilateral and "paint" it (only looping the quadrilateral
% and not the whole image)
for i=1 : IndexesSize(1)
newIm(ourIndexes(i))=BilinearInterpolation(im2,YYY(i), XXX(i));
end
fprintf('press any key to see the result...');
pause;
imshow(newIm);
title('The new mapped Image');
this is the function
function NewVal=BilinearInterpolation(imgB,x,y)
% inpust: ImageB- the pattern image
% x and y the coordinates that locates the inverse mapping
% returns the new color of a spicifc pixel (x',y') in imageA
% SW SE NE NW are neighboors of (x,y) which will give us the new color
SW=imgB(floor(x),floor(y)+1);
SE=imgB(floor(x)+1,floor(y)+1);
NE=imgB(floor(x)+1,floor(y));
NW=imgB(floor(x),floor(y));
% the equations are the same as in the lecture
% S = SE * delta(x) + SW*(1- delta(x))
% N = NE * delta(x) + NW*(1- delta(x))
% V = N * delta(y) + S*(1 - delta(y))
deltax=x-floor(x);
S=SE*deltax+SW*(1-deltax);
N=NE*deltax+NW*(1-deltax);
deltay=1-(y-floor(y));
NewVal=N*deltay+S*(1-deltay);
end
I tried this:
newIm(ourIndexes)=BilinearInterpolation(im2,YYY, XXX);
but of course it didn't work
I tried changing the function to work with (.*) but also had a lot of problems
With a little modification you can make your function accept an array of points.
Firstly as mentioned in the comments change your * to elementwise multiplication .*.
Also you'll need to change the times where you access imgB so that it will accept a list of points. Inputting two vectors (i.e. using the code unchanged) will call all combinations of those points i.e nxn points. Normally I would just use a for loop to avoid this as I am lazy and rarely need to optimise my code that much.
As you specifically don't want to use a loop, the easiest way I know to do this is to replace the calls to the subscripts with the appropriate linear indices using sub2ind. For example, SW=imgB(sub2ind(size(imgB),floor(x),floor(y)+1));
Other than that check that all your vectors have the same long dimension, I think they should but it depends on your input.
I am trying to simulate a network of mobile robots that uses artificial potential fields for movement planning to a shared destination xd. This is done by generating a series of m-files (one for each robot) from a symbolic expression, as this seems to be the best way in terms of computational time and accuracy. However, I can't figure out what is going wrong with my gradient computation: the analytical gradient that is being computed seems to be faulty, while the numerical gradient is calculated correctly (see the image posted below). I have written a MWE listed below, which also exhibits this problem. I have checked the file generating part of the code, and it does return a correct function file with a correct gradient. But I can't figure out why the analytic and numerical gradient are so different.
(A larger version of the image below can be found here)
% create symbolic variables
xd = sym('xd',[1 2]);
x = sym('x',[2 2]);
% create a potential function and a gradient function for both (x,y) pairs
% in x
for i=1:size(x,1)
phi = norm(x(i,:)-xd)/norm(x(1,:)-x(2,:)); % potential field function
xvector = reshape(x.',1,size(x,1)*size(x,2)); % reshape x to allow for gradient computation
grad = gradient(phi,xvector(2*i-1:2*i)); % compute the gradient
gradx = grad(1);grady=grad(2); % split the gradient in two components
% create function file names
gradfun = strcat('GradTester',int2str(i),'.m');
phifun = strcat('PotTester',int2str(i),'.m');
% generate two output files
matlabFunction(gradx, grady,'file',gradfun,'outputs',{'gradx','grady'},'vars',{xvector, xd});
matlabFunction(phi,'file',phifun,'vars',{xvector, xd});
end
clear all % make sure the workspace is empty: the functions are in the files
pause(0.1) % ensure the function file has been generated before it is called
% these are later overwritten by a specific case, but they can be used for
% debugging
x = 0.5*rand(2);
xd = 0.5*rand(1,2);
% values for the Stackoverflow case
x = [0.0533 0.0023;
0.4809 0.3875];
xd = [0.4087 0.4343];
xp = x; % dummy variable to keep x intact
% compute potential field and gradient for both (x,y) pairs
for i=1:size(x,1)
% create a grid centered on the selected (x,y) pair
xGrid = (x(i,1)-0.1):0.005:(x(i,1)+0.1);
yGrid = (x(i,2)-0.1):0.005:(x(i,2)+0.1);
% preallocate the gradient and potential matrices
gradx = zeros(length(xGrid),length(yGrid));
grady = zeros(length(xGrid),length(yGrid));
phi = zeros(length(xGrid),length(yGrid));
% generate appropriate function handles
fun = str2func(strcat('GradTester',int2str(i)));
fun2 = str2func(strcat('PotTester',int2str(i)));
% compute analytic gradient and potential for each position in the xGrid and
% yGrid vectors
for ii = 1:length(yGrid)
for jj = 1:length(xGrid)
xp(i,:) = [xGrid(ii) yGrid(jj)]; % select the position
Xvec = reshape(xp.',1,size(x,1)*size(x,2)); % turn the input into a vector
[gradx(ii,jj),grady(ii,jj)] = fun(Xvec,xd); % compute gradients
phi(jj,ii) = fun2(Xvec,xd); % compute potential value
end
end
[FX,FY] = gradient(phi); % compute the NUMERICAL gradient for comparison
%scale the numerical gradient
FX = FX/0.005;
FY = FY/0.005;
% plot analytic result
subplot(2,2,2*i-1)
hold all
xlim([xGrid(1) xGrid(end)]);
ylim([yGrid(1) yGrid(end)]);
quiver(xGrid,yGrid,-gradx,-grady)
contour(xGrid,yGrid,phi)
title(strcat('Analytic result for position ',int2str(i)));
xlabel('x');
ylabel('y');
subplot(2,2,2*i)
hold all
xlim([xGrid(1) xGrid(end)]);
ylim([yGrid(1) yGrid(end)]);
quiver(xGrid,yGrid,-FX,-FY)
contour(xGrid,yGrid,phi)
title(strcat('Numerical result for position ',int2str(i)));
xlabel('x');
ylabel('y');
end
The potential field I am trying to generate is defined by an (x,y) position, in my code called xd. x is the position matrix of dimension N x 2, where the first column represents x1, x2, and so on, and the second column represents y1, y2, and so on. Xvec is simply a reshaping of this vector to x1,y1,x2,y2,x3,y3 and so on, as the matlabfunction I am generating only accepts vector inputs.
The gradient for robot i is being calculated by taking the derivative w.r.t. x_i and y_i, these two components together yield a single derivative 'vector' shown in the quiver plots. The derivative should look like this, and I checked that the symbolic expression for [gradx,grady] indeed looks like that before an m-file is generated.
To fix the particular problem given in the question, you were actually calculating phi in such a way that meant you doing gradient(phi) was not giving the correct results compared to the symbolic gradient. I'll try and explain. Here is how you created xGrid and yGrid:
% create a grid centered on the selected (x,y) pair
xGrid = (x(i,1)-0.1):0.005:(x(i,1)+0.1);
yGrid = (x(i,2)-0.1):0.005:(x(i,2)+0.1);
But then in the for loop, ii and jj were used like phi(jj,ii) or gradx(ii,jj), but corresponding to the same physical position. This is why your results were different. Another problem you had was you used gradient incorrectly. Matlab assumes that [FX,FY]=gradient(phi) means that phi is calculated from phi=f(x,y) where x and y are matrices created using meshgrid. You effectively had the elements of phi arranged differently to that, an so gradient(phi) gave the wrong answer. Between reversing the jj and ii, and the incorrect gradient, the errors cancelled out (I suspect you tried doing phi(jj,ii) after trying phi(ii,jj) first and finding it didn't work).
Anyway, to sort it all out, on the line after you create xGrid and yGrid, put this in:
[X,Y]=meshgrid(xGrid,yGrid);
Then change the code after you load fun and fun2 to:
for ii = 1:length(xGrid) %// x loop
for jj = 1:length(yGrid) %// y loop
xp(i,:) = [X(ii,jj);Y(ii,jj)]; %// using X and Y not xGrid and yGrid
Xvec = reshape(xp.',1,size(x,1)*size(x,2));
[gradx(ii,jj),grady(ii,jj)] = fun(Xvec,xd);
phi(ii,jj) = fun2(Xvec,xd);
end
end
[FX,FY] = gradient(phi,0.005); %// use the second argument of gradient to set spacing
subplot(2,2,2*i-1)
hold all
axis([min(X(:)) max(X(:)) min(Y(:)) max(Y(:))]) %// use axis rather than xlim/ylim
quiver(X,Y,gradx,grady)
contour(X,Y,phi)
title(strcat('Analytic result for position ',int2str(i)));
xlabel('x');
ylabel('y');
subplot(2,2,2*i)
hold all
axis([min(X(:)) max(X(:)) min(Y(:)) max(Y(:))])
quiver(X,Y,FX,FY)
contour(X,Y,phi)
title(strcat('Numerical result for position ',int2str(i)));
xlabel('x');
ylabel('y');
I have some other comments about your code. I think your potential function is ill-defined, which is causing all sorts of problems. You say in the question that x is an Nx2 matrix, but you potential function is defined as
norm(x(i,:)-xd)/norm(x(1,:)-x(2,:));
which means if N was three, you'd have the following three potentials:
norm(x(1,:)-xd)/norm(x(1,:)-x(2,:));
norm(x(2,:)-xd)/norm(x(1,:)-x(2,:));
norm(x(3,:)-xd)/norm(x(1,:)-x(2,:));
and I don't think the third one makes sense. I think this could be causing some confusion with the gradients.
Also, I'm not sure if there is a reason to create the .m file functions in your real code, but they are not necessary for the code you posted.
I have the following code for calculating the result of a linear combination of Gaussian functions. What I'd really like to do is to vectorize this somehow so that it's far more performant in Matlab.
Note that y is a column vector (output), x is a matrix where each column corresponds to a data point and each row corresponds to a dimension (i.e. 2 rows = 2D), variance is a double, gaussians is a matrix where each column is a vector corresponding to the mean point of the gaussian and weights is a row vector of the weights in front of each gaussian. Note that the length of weights is 1 bigger than gaussians as weights(1) is the 0th order weight.
function [ y ] = CalcPrediction( gaussians, variance, weights, x )
basisFunctions = size(gaussians, 2);
xvalues = size(x, 2);
if length(weights) ~= basisFunctions + 1
ME = MException('TRAIN:CALC', 'The number of weights should be equal to the number of basis functions plus one');
throw(ME);
end
y = weights(1) * ones(xvalues, 1);
for xIdx = 1:xvalues
for i = 1:basisFunctions
diff = x(:, xIdx) - gaussians(:, i);
y(xIdx) = y(xIdx) + weights(i+1) * exp(-(diff')*diff/(2*variance));
end
end
end
You can see that at the moment I simply iterate over the x vectors and then the gaussians inside 2 for loops. I'm hoping that this can be improved - I've looked at meshgrid but that seems to only apply to vectors (and I have matrices)
Thanks.
Try this
diffx = bsxfun(#minus,x,permute(gaussians,[1,3,2])); % binary operation with singleton expansion
diffx2 = squeeze(sum(diffx.^2,1)); % dot product, shape is now [XVALUES,BASISFUNCTIONS]
weight_col = weights(:); % make sure weights is a column vector
y = exp(-diffx2/2/variance)*weight_col(2:end); % a column vector of length XVALUES
Note, I changed diff to diffx since diff is a builtin. I'm not sure this will improve performance as allocating arrays will offset increase by vectorization.