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일

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

추가 답변 (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에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by