Can someone help? I'm trying to learn how to use cellfun and I don't understand it completely.
Here's one way you can use cellfun to solve this problem. I made an anonymous function called isDraw.
isDraw = @(letter) letter=='D'
d = cellfun(isDraw,games)
draws = sum(d)
Use contains to check how many elements == 'D', then sum
draws = sum(contains(games,'D'))
Test | Status | Code Input and Output |
---|---|---|
1 | Fail |
games = {'D','D','A','H','D','H'};
draws = 3;
assert(isequal(how_many_draws(games),draws))
|
2 | Fail |
games = {'D','D'};
draws = 2;
assert(isequal(how_many_draws(games),draws))
|
3 | Fail |
games = {'H','H','A'};
draws = 0;
assert(isequal(how_many_draws(games),draws))
|
4 | Fail |
games = {'D','H','H','A','D','H','H','A','D','H','H','A','D','D'};
draws = 5;
assert(isequal(how_many_draws(games),draws))
|
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!