Run a function into a function

2 次查看(过去 30 天)
Hi I have a function "a" called "function x=problem1(a,b,c,d)" I would like to create a function "b" that call function "a" and asign values to (a,b,c,d). Also, the function "b" have to run at least 6 times just with one call.

采纳的回答

Adrian Quesada
Adrian Quesada 2017-8-21
No, I'm talking about a function to run replace_me 5 times with diferents inputs. Like this:
% code
function problemb
1 try
v=[1 2 3 4];
a=2
b=8
c=6
end
2 try
v=[5 6 7 8];
a=6
c=2
end
3 try
v=[1 2 6 9];
a=2
b=8
c=6
end
In my example, I already define the inputs for each run, so, I just put both .m files together and run the problem one time to have 5 different solutions at once.
  3 个评论
Adrian Quesada
Adrian Quesada 2017-8-21
Thanks for your answer, one more question When I run your code, the answer is:
% code
ans =
1×3 cell array
[1×5 double] [1×5 double] [1×5 double]
How do I do to show the vectors?

请先登录,再进行评论。

更多回答(4 个)

Jan
Jan 2017-8-21
编辑:Jan 2017-8-21
The question is not really clear. I assume that this is very basic and suggest to read the Getting Started chapters to learn the fundamentals of programming. But here a suggestion:
function b
for k = 1:6
a = rand;
b = 17.3;
c = k;
d = a + b + c ^ 2;
x = problem1(a,b,c,d);
disp(x);
end
Now look at the code and explain, what your problem is with any details.

Walter Roberson
Walter Roberson 2017-8-21

Adrian Quesada
Adrian Quesada 2017-8-21
编辑:Adrian Quesada 2017-8-21
Ok, I will try to explain it better. Supose I have the next code:
% code
function w = replace_me(v,a,b,c)
if nargin < 3
b = 0;
end
if nargin < 4
c = b;
end
w = [];
for k = 1:length(v);
if v(k) == a % if a is found,
w = [w,b,c]; % we insert b and c at the end of the current w
else % otherwise,
w = [w,v(k)]; % we insert the original element of v
end
end
end
I will like to create another function that has the input parameters for "function w = replace_me(v,a,b,c)", but it has to try the code with 5 diferent input options at the same time.
  1 个评论
Walter Roberson
Walter Roberson 2017-8-21
Your existing code already handles the possibility that v will be a vector of length 5. Your existing code has b and c be optional. Are you talking about 5 different a values? If so, then would it be acceptable to just change
if v(k) == a
to
if ismember(v(k), a)
?

请先登录,再进行评论。


Adrian Quesada
Adrian Quesada 2017-8-21
Thaks for your help

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by