Borrar filtros
Borrar filtros

how to define an anonymous function in spmd block?

8 visualizaciones (últimos 30 días)
Samruddhi Pednekar
Samruddhi Pednekar el 11 de Abr. de 2016
Respondida: sam0037 el 14 de Abr. de 2016
I want to define sinx function in spmd block. how can I do it...it is giving an error you cant define an anonymous function in an spmd block
  1 comentario
Kirby Fears
Kirby Fears el 11 de Abr. de 2016
Editada: Kirby Fears el 11 de Abr. de 2016
Did you try writing a function instead of an anonymous function?
For example, the anonymous function:
@(x) x/2
could be turned into a regular function:
function result = divideByTwo(x)
result = x/2;
Then you could use the function by name when calling spmd.

Iniciar sesión para comentar.

Respuestas (1)

sam0037
sam0037 el 14 de Abr. de 2016
Hi,
As per this documentation link , the body of an spmd statement cannot define an anonymous function. However, it can reference an anonymous function by means of a function handle.
For example consider the following code where the anonymous function is declared outside spmd block and is referred to successfully inside the spmd block with its handle 'f'.
f = @(x) sin(x);
spmd
sum = 0;
for i = 1:10
sum = sum+ f(i);
end
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by