Multiple Output Anonymous function: Using one output value for another output.
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Matt J
il 2 Nov 2016
Modificato: Matt J
il 2 Nov 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!