Run a function into a function

2 ビュー (過去 30 日間)
Adrian Quesada
Adrian Quesada 2017 年 8 月 21 日
回答済み: Adrian Quesada 2017 年 8 月 21 日
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?
Walter Roberson
Walter Roberson 2017 年 8 月 21 日
celldisp(ans)

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by