필터 지우기
필터 지우기

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

조회 수: 1 (최근 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에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by