Variable B in a table (type = double) needs to calculated based on Variable A in the same table (type = double), but the key is that Variable B is calculated based on the value of Variable A in both the current row and the previous row. How please ?
Ältere Kommentare anzeigen
Variable B in a table (type = double) needs to calculated based on Variable A in the same table (type = double), but the key is that Variable B is calculated based on the value of Variable A in both the current row and the previous row. I may have to do this in a 'for' loop but I have tried and failed. I tried to use the index 'n' in the for loop as follows:
for n=1:5
a_small.test(n) = a_small.Flow(n)- a_small.Flow(n-1)
end
Any ideas ?
Antworten (1)
James Tursa
am 4 Nov. 2016
Bearbeitet: James Tursa
am 4 Nov. 2016
When n=1 you are using an index of 0 which will cause an error. What do you want to do for the 1st element since there is no 0'th element to work with? Leave it alone? E.g., start your indexing at 2 instead?
for n=2:5
a_small.test(n) = a_small.Flow(n)- a_small.Flow(n-1)
end
3 Kommentare
Hydrology-1
am 4 Nov. 2016
James Tursa
am 4 Nov. 2016
Bearbeitet: James Tursa
am 4 Nov. 2016
Well, to do a simple difference between adjacent elements you could use the diff function:
a_small.test = [0;diff(a_small.Flow)];
Hydrology-1
am 5 Nov. 2016
Bearbeitet: James Tursa
am 5 Nov. 2016
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!