how to index next element in a vector/ row in a table
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical 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!