function chaining when one function has multiple outputs
48 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Simon
el 19 de Ag. de 2024 a las 20:27
Comentada: Simon
el 20 de Ag. de 2024 a las 17:20
Similar question has been asked before, but I would ask the question in a more specific manner. If the function in the input end has more than one output, there will be the error 'Not enough input arguments'. Is there a neat way to make chained function out of that situation?
f(g) % only the first output of g is kept as input for f
function [x, y] = g()
x = 1;
y = 2;
end
function out = f(x, y)
out = x+y;
end
Storing the outputs of the first function in two variables could be a solution, but I like to see how chaining could be done in this kind of scenario for the sake of learning.
[x, y] = g();
f(x, y)
2 comentarios
Stephen23
el 20 de Ag. de 2024 a las 5:24
Editada: Stephen23
el 20 de Ag. de 2024 a las 5:58
"Is there a neat way to make chained function out of that situation? "
No. The MATLAB apporoach is to call the two functions on two lines: this works because MATLAB's function outputs are demand driven. A discussion of why this behavior is required:
Respuesta aceptada
Steven Lord
el 19 de Ag. de 2024 a las 20:48
Suppose you had a function that could be called with either 1 output or more outputs, and a function that can accept 1 input or multiple inputs. If you chained those two so that the output(s) of the first of those functions were passed into the second function, how should MATLAB know with how many outputs to call the first function? To give an example (not necessarily a realistic example but just to illustrate the situation):
A = magic(4);
L = lu(A);
oneOutputCase = max(L)
[L, U] = lu(A);
twoOutputCase = max(L, U)
chainCase = max(lu(A))
Were you expecting the twoOutputCase output from the chainCase call? The lu function can return up to five outputs (though the fourth and fifth are only supported for sparse inputs, but the first three are supported for both sparse and full inputs) and the max function can accept (as data inputs) one or two inputs. How would you expect MATLAB to distinguish between those two cases?
Más respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown 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!