Azzera filtri
Azzera filtri

Run a function into a function

2 visualizzazioni (ultimi 30 giorni)
Adrian Quesada
Adrian Quesada il 21 Ago 2017
Risposto: Adrian Quesada il 21 Ago 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.

Risposta accettata

Adrian Quesada
Adrian Quesada il 21 Ago 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 Commenti
Adrian Quesada
Adrian Quesada il 21 Ago 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?

Accedi per commentare.

Più risposte (4)

Jan
Jan il 21 Ago 2017
Modificato: Jan il 21 Ago 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 il 21 Ago 2017

Adrian Quesada
Adrian Quesada il 21 Ago 2017
Modificato: Adrian Quesada il 21 Ago 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 Commento
Walter Roberson
Walter Roberson il 21 Ago 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)
?

Accedi per commentare.


Adrian Quesada
Adrian Quesada il 21 Ago 2017
Thaks for your help

Categorie

Scopri di più su Historical Contests in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by