フィルターのクリア

Split digits into 2 matrices

1 回表示 (過去 30 日間)
loes henstra
loes henstra 2021 年 6 月 2 日
編集済み: Walter Roberson 2021 年 6 月 2 日
I am trying to solve a card problem.
I get two hands as an imput (Let's say 5H 5C 6S 7S 10D and 2C 3S 6S 7S 10D)
For this I want to split suit and number and put them in 2 different matrices. (I ofcourse number the suits first).
I have found ways to split the digits of a number, but not to put them in 2 different matrices.

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 6 月 2 日
編集済み: Walter Roberson 2021 年 6 月 2 日
Different approaches:
cards = {'5H' '5C' '6S' '7S' '10D' 'QC' '3S' 'KS' '7S' '10D'}
cards = 1×10 cell array
{'5H'} {'5C'} {'6S'} {'7S'} {'10D'} {'QC'} {'3S'} {'KS'} {'7S'} {'10D'}
ranks1 = cellfun(@(c) c(1:end-1), cards, 'uniform', 0)
ranks1 = 1×10 cell array
{'5'} {'5'} {'6'} {'7'} {'10'} {'Q'} {'3'} {'K'} {'7'} {'10'}
suits1 = cellfun(@(c) c(end), cards, 'uniform', 0)
suits1 = 1×10 cell array
{'H'} {'C'} {'S'} {'S'} {'D'} {'C'} {'S'} {'S'} {'S'} {'D'}
ranks2 = regexprep(cards, '.$', '', 'once')
ranks2 = 1×10 cell array
{'5'} {'5'} {'6'} {'7'} {'10'} {'Q'} {'3'} {'K'} {'7'} {'10'}
suits2 = regexp(cards, '.$', 'match', 'once')
suits2 = 1×10 cell array
{'H'} {'C'} {'S'} {'S'} {'D'} {'C'} {'S'} {'S'} {'S'} {'D'}
ranks3 = extractBefore(cards, lettersPattern(1) + lineBoundary)
ranks3 = 1×10 cell array
{'5'} {'5'} {'6'} {'7'} {'10'} {'Q'} {'3'} {'K'} {'7'} {'10'}
suits3 = extract(cards, lettersPattern(1) + lineBoundary)
suits3 = 1×10 cell array
{'H'} {'C'} {'S'} {'S'} {'D'} {'C'} {'S'} {'S'} {'S'} {'D'}
ranks4 = extract(cards, asManyOfPattern(characterListPattern("A1234567890JQK"),1))
ranks4 = 1×10 cell array
{'5'} {'5'} {'6'} {'7'} {'10'} {'Q'} {'3'} {'K'} {'7'} {'10'}
suits4 = extract(cards, characterListPattern("CDHS"))
suits4 = 1×10 cell array
{'H'} {'C'} {'S'} {'S'} {'D'} {'C'} {'S'} {'S'} {'S'} {'D'}
ranks5 = regexp(cards, '[0-9AJQK]+', 'match', 'once')
ranks5 = 1×10 cell array
{'5'} {'5'} {'6'} {'7'} {'10'} {'Q'} {'3'} {'K'} {'7'} {'10'}
suits5 = regexp(cards, '[CDHS]', 'match', 'once')
suits5 = 1×10 cell array
{'H'} {'C'} {'S'} {'S'} {'D'} {'C'} {'S'} {'S'} {'S'} {'D'}

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by