Calling multiple outputs of a function into new function

조회 수: 9 (최근 30일)
Asim Ismail
Asim Ismail 2017년 5월 13일
댓글: Asim Ismail 2017년 5월 14일
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
Asim Ismail
Asim Ismail 2017년 5월 14일
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 2017년 5월 14일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 5월 14일
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
Stephen23
Stephen23 2017년 5월 14일
편집: Stephen23 2017년 5월 14일
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 2017년 5월 14일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by