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

1 回表示 (過去 30 日間)
regina
regina 2015 年 10 月 21 日
回答済み: TastyPastry 2015 年 10 月 21 日
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 Center および File ExchangeConway'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