cell2mat not working when cells are different lengths and what to find combinations

2 次查看(过去 30 天)
I want to obtain a matrix with all the player Vs player combinations. Whe i have my players names A, B, C and D the code below works perfectly. However whe i give my players names such as Alice, Ben, Cody and David the following code does not work. Is there something i can add to make it work?
function[changes] = calculating_changes(rating,position)
changes = containers.Map;
for player = keys(rating)
changes(player{1}) = 0;
end
all_players=cell2mat(keys(rating))
%cell2mat converts the cell array to an ordinary array
%puts the changes in a list with their corresponding player
match_table = nchoosek(all_players,2)
%nchoosek is used to get a marix with all player vs player combinations
end
This code the output as followed when using ABCD I'd like it to do the same but with names not letters
all_players =
'ABCD'
match_table =
6×2 char array
'AB'
'AC'
'AD'
'BC'
'BD'
'CD'

回答(1 个)

Shadaab Siddiqie
Shadaab Siddiqie 2020-12-9
Form my understanding you want to obtain a matrix with all the player Vs player combinations. But since player name might not be of same lenght, you can create a player Vs player cell array. This can be done by removing
all_players=cell2mat(keys(rating))
and replacing
match_table = nchoosek(all_players,2)
with
match_table = nchoosek(keys(rating),2)
If you want you can convert "match_table" to any format you like. Refere cell2mat and nchoosek for more information.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by