Calling multiple outputs of a function into new function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Asim Ismail
el 13 de Mayo de 2017
Comentada: Asim Ismail
el 14 de Mayo de 2017
I have a function with multiple outputs, now i want to call them into a new function and apply some formulas to them.
Applying formulas on each output one by one seems impossible, so any simplest way to do this?
Thanks!
5 comentarios
Jan
el 14 de Mayo de 2017
@asim: I see a general problem here. Your explanations are most likely clear, if somebody knows, what you are doing already. But the readers do not have the faintest idea.
Please do not only describe in words, what you are doing, but post the code. This will help us to understand.
Respuesta aceptada
Jan
el 14 de Mayo de 2017
With some bold guessing:
[out1, out2, ....., outn] = fun(in1, in2)
but it didnt give me any output,except the first one,
This might mean, that you define the function as
function [out1, out2, ....., outn] = fun(in1, in2)
...
and call it like this:
fun(in1, in2)
but catching the outputs is obligatory:
[out1, out2, ....., outn] = fun(in1, in2)
Then
i removed the function
might mean, that you removed the top line of the function definition "function [out1, out2, ....., outn] = fun(in1, in2)", such that the code becomes a script. This is a bad idea, because functions are much cleaner and safer. Nevertheless, it might work.
i start to call them one by one in another function. but this
method takes too long, my question is how to avoid this method
and use an efficient way.
I cannot guess, what "call them one by one in another function" means and without seeing the code it is impossible to recognize, why it is taking "too long". Perhaps a pre-allocation is forgotton or teh code can be vectorized, maybe expensive operations are performed repeatedly.
I do not even know what "too long" means - weeks or milliseconds?
So, please, post the code, provide some useful inputs, replace irrelevant input data by radnom arrays and give us a chance to reconsider, what you are doing. Then it will be possible to solve your specific problem.
5 comentarios
Stephen23
el 14 de Mayo de 2017
Editada: Stephen23
el 14 de Mayo de 2017
Aha, this seems to be a case of the X-Y problem. Maybe we can finally start to help you. Here is one easy solution to the question you pose in your comment above:
M = [5,3;5,2;2,5;5,3;2,3;3,6]; % the matrix sizes.
R = size(M,1);
C = cell(R,1);
for k = 1:R
C{k} = rand(M(k,:)); % generate the matrices using those sizes
end
and then you just need to pass one variable C to your next function:
D = cell(size(C));
for k = 1:numel(C)
D{k} = C{k}>0.5;
end
And D is the output, a cell array of logical arrays, where each logical array shows the conditions that you test for.
Did you see how I just managed to solve your problem by using good program design, in particular I did not try to handle many individual numbered variables (which is very bad program design), but simply put all of the matrices into one cell array. And of course passing one cell array between functions is trivially easy!
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!