MATLAB: How modifying the input of a function : from (unique) vector to matrix

MATLABmatrix_manipulation

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

Best Answer

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