Using Length and Find

5 次查看(过去 30 天)
Caroline Prekker
Caroline Prekker 2020-7-25
I am trying to find the sum of numbers in a matrix by the 4th inning (the data is from multiple softball games) so that I can compare them to see who is in the lead by the 4th inning. I have tried the code below and cannot figure out how to get it to work. The two that aren't working are the oppPtsAfter4 and auPtsAfter4
%Auburn record after all games
auNumWins = length(find(gamePts(:,1)<gamePts(:,2)));
fprintf('Auburn season record: %d-%d\n',auNumWins,auRows-auNumWins)
%Auburn record at the end of the 4th inning
oppPtsAfter4 = length(find(oppPoints,4:8));
auPtsAfter4 = length(find(auPoints,13:17));

回答(1 个)

Walter Roberson
Walter Roberson 2020-7-25
When you use a numeric second parameter for find(), then you are controlling the number of reponses you want returned. It does not make sense to put a vector at that point.
Wouldn't you be wanting to look at something like oppPoints(:,4) and auPoints(:,4) ?
auNumWins = length(find(gamePts(:,1)<gamePts(:,2)));
A much more compact way of writing that is
auNumWins = nnz(gamePoints(:,1) < gamePoints(:,2));
  3 个评论
Caroline Prekker
Caroline Prekker 2020-7-25
that portion is also not the problem I am having, that was given to us and works fine. The issue I am having is finding the sum of points after 4 innings of each game by both teams because it is a matrix
Walter Roberson
Walter Roberson 2020-7-25
Is the matrix a list of points per inning? If so then sum(oppPoints(:,1:4), 2) would give the sum for the first 4 innings.
auPoints,13:17
Is your data file set up so that for each game you have a list of 9 values that are opponent runs in one inning, followed by a list of 9 values that are Auburn runs in one inning? If so then it could make sense to access columns 1:4 (the opponent) and 10 to 13 (auburn). However if the data has already been split into two matrices, as hinted by oppPoints and auPoints being different arrays, then it would not appear to make sense to look in columns 10:13 of auburn data.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by