# Running anonymous function in MATLAB for sub-mesh.

31 views (last 30 days)
maubars on 15 Oct 2020 at 14:16
Answered: Sulaymon Eshkabilov on 18 Oct 2020 at 23:09
Is it possble to vectorize annonymous functions in Matlab. Suppossing I wish to place and run an annoymous repeatedly on designated positions of a big, mesh, running the function at specific chunks of the mesh. For example, if I have a 2D meshgrid of size 1000 by 1000 mesh and I wish to run a annoymous function repeatedly along the centre line skiping random steps like x = [20 30 50 100 ..... and y = [0 0 0 .. to generate multiple shapes or forms, like circles around some centerline without the use of a loop. How can i run such a function at the randow x and y coordinate points?The figure below displays a desirable output from such a scenerio. Take note that the x cordinates spacing between the circle centers are not regular.
N=200
[X,Y]= meshgrid(linspace(1,1000,N),linspace(-500,500,N));
% I skipped a step for selectin the chunks here...
% Submesh selection
x1=X(:,20:30); x2=X(:,30:50); x3=X(:,50:100); x4=X(:,100:end);
y1=Y(:,20:30); y2=Y(:,30:50); y3=Y(:,50:100); y4=Y(:,100:end);
% I select the center line positions
centers_x = x1(100,5);
centers_y = y1(100,5);
% the below functions should be mapped to the cicle based on the centers line positions
x = func1(centers_x);
y = func2(centers_y);
plot(x, y, 'b-', 'LineWidth', 2);
hold on; Show 1 older comment
maubars on 16 Oct 2020 at 4:41
Thanks. I read the article. The article mentions the iteration of annonymous functions which are stored in MATLAB using the symbolical tool engine. Does it mean I select the centers and I use the centers as symbols, then I run the same fuction on the other centers?
Walter Roberson on 16 Oct 2020 at 5:15
You do not need to do symbolic work. I was especially referring you to the paragraphs starting with "When an anonymous function is built," which talks about the variables being saved along with the handle.
Other than the saved variables, anonymous functions are pretty much the same as regular functions.
and rewriting it to
function result = func1(centers_x, varargin)
if nargin > 1
angles = varargin{2};
end
end
and then right at the point you would have defined the anonymous function, put in a call
to cause the values of radius and angles to be saved inside func1.
and after that make regular calls to func1, passing in only one argument.
Here is the important part: the anonymous function is exactly as vectorized as the full function would be, no more and no less.
There is nothing magical or restricted about vectorization in anonymous function handles: either what you pass in to the anonymous function is compatible size with the captured variables, or else it isn't and you get an error -- exactly the same as if it were a regular function that had (somehow) had access to the captured variables.
It's all the same as regular functions, other than the way that values of variables are captured at the time of the definition of the anonymous function.
maubars on 17 Oct 2020 at 2:48
Thanks on the comment and info. I think I will have to redajust the code accordingly.

Sulaymon Eshkabilov on 18 Oct 2020 at 23:09
Hi,
Here is one of the viable solutuons to your exercise using the function option:
clearvars
% Number of circles to draw
N=10;
% The center line positions along x and y axes:
C_x = linspace(0, 200, N);
C_y = zeros(size(C_x));
R=10;
angles=linspace(0, 2*pi, 360);
[X, Y] = Circle(C_x, C_y, R);
for ii=1:numel(C_x)
plot(X(ii,:), Y(ii,:), 'LineWidth', 2); axis equal
hold all;
text(C_x(ii), C_y(ii), num2str(ii))
end
grid on
% Function file within M-file:
function [X, Y] = Circle(C_x, C_y,varargin)
if nargin ==2
angles=linspace(0, 2*pi, 100);
R=10;
elseif nargin ==3
angles=linspace(0, 2*pi, 100);
R=varargin{1};
else
R=varargin{1};
angles=varargin{2};
end
X= (R*sin(angles)+C_x(:));
Y= (R*cos(angles)+C_y(:));
end