Effacer les filtres
Effacer les filtres

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

15 vues (au cours des 30 derniers jours)
Ross Hanna
Ross Hanna le 1 Mar 2018
Modifié(e) : Stephen23 le 1 Mar 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

Réponse acceptée

Bob Thompson
Bob Thompson le 1 Mar 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 commentaire
Ross Hanna
Ross Hanna le 1 Mar 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by