Run a function into a function

1 vue (au cours des 30 derniers jours)
Adrian Quesada
Adrian Quesada le 21 Août 2017
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.

Réponse acceptée

Adrian Quesada
Adrian Quesada le 21 Août 2017
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 commentaires
Adrian Quesada
Adrian Quesada le 21 Août 2017
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?
Walter Roberson
Walter Roberson le 21 Août 2017

Connectez-vous pour commenter.

Plus de réponses (4)

Jan
Jan le 21 Août 2017
Modifié(e) : Jan le 21 Août 2017
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 le 21 Août 2017

Adrian Quesada
Adrian Quesada le 21 Août 2017
Modifié(e) : Adrian Quesada le 21 Août 2017
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 commentaire
Walter Roberson
Walter Roberson le 21 Août 2017
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)
?

Connectez-vous pour commenter.


Adrian Quesada
Adrian Quesada le 21 Août 2017
Thaks for your help

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by