How to use the unique command in a loop and how to filter data?

1 次查看(过去 30 天)
I have a table of data for the year 2001, i need goals less than or equal to 12 and then to count the number of different teams that only fit in this category (have never scored higher than 12 goals in 2001), so far:
t= length(unique(guest_team(i)));
y=0
for i=1:games;
if 2001==year(i) & t & 12>=guest_goals(i) ;
y=y+1;
end
end
however, this just gives me the number of instances any team scores less than or equal to 12, any help would be greatly appreciated thanks!

回答(1 个)

TastyPastry
TastyPastry 2015-10-21
If your data is organized something like:
Year Team Goals
2001 1 14
2001 2 12
2002 3 8
2003 1 9
2002 1 16
...
Where Year, Team and Score are separate vectors but each index corresponds to a game, then
numTeams = numel(unique(guest_team(year == 2001 && guest_goals <= 12)));
You're looking for games in 2001, goals less than 12. That creates your mask for guest_team. Then, the output of unique() will only find one instance of each team, numel() finds how many teams there are.

类别

Help CenterFile Exchange 中查找有关 Conway's Game of Life 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by