How modifying the input of a function : from (unique) vector to matrix ?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Rik
2018 年 11 月 29 日
編集済み: Rik
2018 年 11 月 30 日
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 件のコメント
Guillaume
2018 年 11 月 29 日
編集済み: Guillaume
2018 年 11 月 30 日
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
2018 年 11 月 30 日
No problem, if I got a euro for every time my name was spelled with a c I'dd be a rich man ;)
その他の回答 (1 件)
jaouad daoudi
2018 年 11 月 30 日
4 件のコメント
Rik
2018 年 12 月 3 日
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)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!