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

13 views (last 30 days)
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

Accepted Answer

Bob Thompson
Bob Thompson on 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 Comment
Ross Hanna
Ross Hanna on 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!