random selection for running functions sequentially

1 view (last 30 days)
Hello; I have a problem with different data, which I can't put them in a matrix. for example, I have 3 houses with different appliances and an optimization problem for each of them. I want put each optimization problem in a function. for example,OF1, OF2 & OF3. the problem is a game between this houses , and in each iteration of that, I've used: "randperm(1,3)" to arrange the sequence of players. I mean the sequence in iteration one is 3,2,1 and in next one is may be 2,3,1. I don't know how should I arrange the sequence of runninf the functios OF1, OF2 &OF3 ? could you please help me? Thanks so much in advance.

Accepted Answer

Guillaume
Guillaume on 1 Sep 2014
I'm afraid it's not very clear what you are asking (there's a fair number of typos). If I understand correctly, you're asking how to call your functions OF1, OF2 and OF3 in the same order as the random permutation you've generated?
If that is the case, store your functions as handles in a cell array and index that cell array with your permutations.
ofs = {@OF1, @OF2, @OF3};
housepermutations = randperm(3); %randperm(1,3) as in your example is not valid
for hp = housepermutations
ofs{hp}(arg1, arg2, ...); %replace arg1, arg2, ... with the proper arguments
end
  3 Comments
Guillaume
Guillaume on 3 Sep 2014
Your first problem is that Home1 is a script not a function. Change it into a function for my solution to work. You'll have to figure out what are the input and output arguments of the script to define the function.
If you can't do that, you'll have to use a switch statement to call the proper script:
housepermutations = randperm(4);
for hp = housepermutation
switch hp
case 1
Home1
case 2
Home2
case 3
Home3
case 4
Home4
end
%do something with the result
end
Maryamm Bodaghi
Maryamm Bodaghi on 3 Sep 2014
Edited: Maryamm Bodaghi on 3 Sep 2014
Thanks so much, I'm trying to do that.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!