Calling multiple outputs of a function into new function

6 Ansichten (letzte 30 Tage)
Asim Ismail
Asim Ismail am 13 Mai 2017
Kommentiert: Asim Ismail am 14 Mai 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 Kommentare
Asim Ismail
Asim Ismail am 14 Mai 2017
First i made a function
[out1, out2, ....., outn] = fun(in1, in2)
but it didnt give me any output,except the first one, so i removed the function then it gave me a series of outputs (out1, out2, ....., outn) and 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.
Jan
Jan am 14 Mai 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 14 Mai 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 Kommentare
Stephen23
Stephen23 am 14 Mai 2017
Bearbeitet: Stephen23 am 14 Mai 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!
Asim Ismail
Asim Ismail am 14 Mai 2017
Thanks @Stephen, _ Did you see how I just managed to solve your problem by using good program design,_ Thats what i want to do, but i am bit new to matlab, so im learning the things coming to my way and trying to avoid the mistakes in future. Anyway thank you guys for helping me out.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by