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

조회 수: 14 (최근 30일)
Ross Hanna
Ross Hanna 2018년 3월 1일
편집: Stephen23 2018년 3월 1일
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

채택된 답변

Bob Thompson
Bob Thompson 2018년 3월 1일
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
Ross Hanna
Ross Hanna 2018년 3월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by