How modifying the input of a function : from (unique) vector to matrix ?
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
Accepted Answer
More Answers (1)
jaouad daoudi
on 30 Nov 2018
0 votes
4 Comments
Rik
on 30 Nov 2018
It appears I forgot to put in the return after the loop, see my edited code.
Also, you posted this comment in the answer field. Please use the comment field next time, as the order of answers can change, which may cause confusion for future readers.
jaouad daoudi
on 3 Dec 2018
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)
jaouad daoudi
on 5 Dec 2018
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!