How modifying the input of a function : from (unique) vector to matrix ?
1 view (last 30 days)
Show older comments
Hi,
I have a script that compute an index. The input of the function is:
(i) the vector of weight of each player (l) and;
(ii) the quota (limit).
It works well for a single vector. However, I need help to modify the code in order to compute the index for several cases (several vectors) (the quota "limit" stay always invariable that we specify in the beginning (ex. 50 or 40 or 20...)). In other words, I want that the input will be:
(i) the matrix summarizing several vectors of weight of each player (l) and;
(ii) the quota (limit).
Example:
Instead of doing the computation on l1 = [20 30 15 25 10] then on l2 = [49 2 49 0 0], then l3 ...., I would like that my input in the function will be a matrix with several lines like for example :
(i) L = [20 30 25 25; 49 2 49 0 0; 20 30 15 25 10; 49 2 49 0 0].
(ii) limit = 50
Thank you in advance for your help and for you support.
function [shapley]=shapley(l, limit)
n = length(l);
C = generate_all_coalitions(n);
s = C*l';
shapley = zeros(n,1);
for party = 1:n
sizeT = 0;
for k = 1:length(s)
row = C(k,:);
modrow = row;
modrow(party) = 0;
if row(party)
if (row*l' > limit && modrow*l' <= limit)
sizeT = sum(row);
shapley(party) = shapley(party) + factorial(sizeT - 1)*factorial(n-sizeT)/factorial(n);
end
end
end
end
end
function [c] = generate_all_coalitions(n)
c = zeros(1,n);
for k = 1:(2^n-1)
c(k+1,:) = convert_to_binary(k,n);
end
end
function [x] = convert_to_binary(n,max)
x = zeros(1,max);
for k = max-1:-1:0
if n - 2^k >= 0
n = n - 2^k;
x(k+1) = 1;
else
x(k+1) = 0;
end
end
end
0 Comments
Accepted Answer
Rik
on 29 Nov 2018
Edited: Rik
on 30 Nov 2018
You can always add a recursive call in case of a matrix input. Also, to keep your code understandable you should write a short block that describes the goal of the function and it's inputs and outputs. The rest of your function should have explanatory comments, so other people can understand it as well (future you is another person as well). As final remarks: don't ignore m-lint warnings (they are very useful), avoid lower-case L as a variable name (as it is easily confused with numeric 1 when reading), and very importantly: don't use your function name as the name of your output variable.
function shapley_output=shapley(player_weight, limit)
if size(player_weight,1)>1
shapley_output=shapley(player_weight(1,:), limit);
for n=2:size(player_weight,1)
shapley_output(:,n)=shapley(player_weight(n,:), limit);
end
return
end
n = numel(player_weight);
C = generate_all_coalitions(n);
s = C*player_weight';
shapley_output = zeros(n,1);
for party = 1:n
for k = 1:length(s)
row = C(k,:);
modrow = row;
modrow(party) = 0;
if row(party)
if (row*player_weight' > limit && modrow*player_weight' <= limit)
sizeT = sum(row);
shapley_output(party) = shapley_output(party) + ...
factorial(sizeT - 1)*factorial(n-sizeT)/factorial(n);
end
end
end
end
end
function [c] = generate_all_coalitions(n)
c = zeros(1,n);
for k = 1:(2^n-1)
c(k+1,:) = convert_to_binary(k,n);
end
end
function [x] = convert_to_binary(n,max)
x = zeros(1,max);
for k = max-1:-1:0
if n - 2^k >= 0
n = n - 2^k;
x(k+1) = 1;
else
x(k+1) = 0;
end
end
end
2 Comments
Guillaume
on 29 Nov 2018
Edited: Guillaume
on 30 Nov 2018
With regards to Rik's point about documenting what a function does and what are its inputs and outputs, see this example I posted not too long ago.
edit: I apologise Rik, for mispelling your name.
Rik
on 30 Nov 2018
No problem, if I got a euro for every time my name was spelled with a c I'dd be a rich man ;)
More Answers (1)
jaouad daoudi
on 30 Nov 2018
4 Comments
Rik
on 3 Dec 2018
You can use xlsread and xlswrite to read from and write to excel files. Their documentation pages will list several examples. (note that empty rows and empty columns are generally ignored by xlsread, so you should keep that in mind)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!