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

3 views (last 30 days)
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'

Answers (1)

Shadaab Siddiqie
Shadaab Siddiqie on 9 Dec 2020
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.

Categories

Find more on Data Type Conversion 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!