8 views (last 30 days)

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

func1 = @(centers_x) (radius*cos(angles)+centers_x);

func2 = @(centers_y) (radius*sin(angles)+centers_y);

x = func1(centers_x);

y = func2(centers_y);

plot(x, y, 'b-', 'LineWidth', 2);

hold on;

Sulaymon Eshkabilov
on 18 Oct 2020

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

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 4 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1063228

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1063228

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1063273

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1063273

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1063293

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1063293

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1065383

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/614993-running-anonymous-function-in-matlab-for-sub-mesh#comment_1065383

Sign in to comment.