How modifying the input of a function : from (unique) vector to matrix ?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
jaouad daoudi
2018년 11월 30일
0 개 추천
댓글 수: 4
Rik
2018년 11월 30일
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
2018년 12월 3일
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)
jaouad daoudi
2018년 12월 5일
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!