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 ?
Mostra commenti meno recenti
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 ?
Risposte (1)
James Tursa
il 4 Nov 2016
Modificato: James Tursa
il 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 Commenti
Hydrology-1
il 4 Nov 2016
James Tursa
il 4 Nov 2016
Modificato: James Tursa
il 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
il 5 Nov 2016
Modificato: James Tursa
il 5 Nov 2016
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!