Using Length and Find

5 visualizaciones (últimos 30 días)
Caroline Prekker
Caroline Prekker el 25 de Jul. de 2020
Comentada: Walter Roberson el 25 de Jul. de 2020
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));

Respuestas (1)

Walter Roberson
Walter Roberson el 25 de Jul. de 2020
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 comentarios
Caroline Prekker
Caroline Prekker el 25 de Jul. de 2020
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 el 25 de Jul. de 2020
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by