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 ?
显示 更早的评论
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 ?
回答(1 个)
James Tursa
2016-11-4
编辑:James Tursa
2016-11-4
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 个评论
Hydrology-1
2016-11-4
James Tursa
2016-11-4
编辑:James Tursa
2016-11-4
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
2016-11-5
编辑:James Tursa
2016-11-5
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!