Borrar filtros
Borrar filtros

how to index next element in a vector/ row in a table

15 visualizaciones (últimos 30 días)
Ross Hanna
Ross Hanna el 1 de Mzo. de 2018
Editada: Stephen23 el 1 de Mzo. de 2018
Hi all
Sorry for such a simple question. I am trying to average some data, and i need a way to write in code;
while the vector V is larger than the the next element in the vector (V+1), take the average of the corresponding values (in the same row of the table, but 2 column left) F, until V is not equal to V-1.
Then repeat for rows 1:end.
The problem is am having is i am unsure how to write (n+1) like i have seen in maths books.
Any help would be greatly appreciated.
Thanks
Ross

Respuesta aceptada

Bob Thompson
Bob Thompson el 1 de Mzo. de 2018
Ok, I'm not entirely sure what exactly you're trying to accomplish, since you seem to be looking forward for greater values, but then looking backwards for equal values.
The basics of indexing, however, can be explained. Assuming V is an array of doubles then you can examine certain values by following the array name with a coordinate location: V(row,col).
For a basic attempt at your while loop, the indexing would look something like this:
I = 1
counter = 0
while V(I,1)<=V(I+1,1) % Examines values of the first column, row I to see if it is less than or equal to the next row, I+1
counter = counter+1; % Independent counter for matrix of other values, incase you don't want to start on the first row with I
results(counter) = V(I,3); % Pulls value of V from two columns over on row I
end
averageresult = mean(results); % Average the completed table
Be forewarned that there are a number of potential problems with this code, specifically if it reaches the end of the file and doesn't break the loop. While loops can be very powerful, but can also fail to break if not set up properly.
  1 comentario
Ross Hanna
Ross Hanna el 1 de Mzo. de 2018
Thanks for the reply. It was the use of counter i was missing.
Essentially, i am trying to average all values of F when V is the same for all. So, for example, if i have 4 rows with V = -36.08, then i would like to average all of the corresponding values for F.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by