Multiple Output Anonymous function: Using one output value for another output.
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have an anonymous function that gives two output and what it does is takes an index 'x' to a vector, 'rowValues', and then gives two outputs where the first one is all the non-zero elements in a 9 element window centered at 'x' and the second output is a vector of length of first output and where all elements are just the element present at the index 'x'.
func=@(x) deal(nonzeros(rowValues(x-4:x+4)),...
repmat(rowValues(x),1,length(nonzeros(rowValues(x-4:x+4))));
As you can see, I am using the length of the first output for creating the second output but I don't think this is an efficient way of doing this because "nonzeros(rowValues(x-4:x+4))" is being evaluated twice. Is there any way that I can use some kind of a handle for the first output to use for the second one? Another question in extension is that, in which order does matlab calculate the output values, is it from left to right or right to left? Any help would be greatly appreciated!!
0 comentarios
Respuesta aceptada
Matt J
el 2 de Nov. de 2016
Editada: Matt J
el 2 de Nov. de 2016
Have func() reference a local function, where you are free to use multiple lines and to recycle intermediate calculations:
func=@(x) localfun(x,rowValues);
function [out1,out2]=localfun(x,rowValues)
out1=nonzeros(rowValues(x-4:x+4));
out2=out1;
out2(:)=rowValues(x);
end
Another question in extension is that, in which order does matlab calculate the output values, is it from left to right or right to left?
Not sure, but I suspect the JIT makes that decision in a very hard-to-predict way. It's also not something I'd rely on to be MATLAB version independent.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!