Multiple Output Anonymous function: Using one output value for another output.
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Matt J
2016-11-2
编辑:Matt J
2016-11-2
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!