1 view (last 30 days)

I have a difference array named 'd'in which there are negative as well as positive differences, calculated by taking difference of an array named 'gFx' .

I will store first value=0 in a separate array named filter. Then I have to compare which elements in 'd' are positive one by one and store them in filter value according to equation written below.

filter(k)={ filter(k-1)+d(k), if d(k)d(k-1)>0

d(k), if d(k)d(k-1)<=0}

this is what i want to implement. where d(k) is difference of two elements of gFx. d(k)d(k-1)>0 means if two consecutive differences are positive then place d(k) and previous filter value in next index of filter. if d(k)<0 then place the value of d(k) in filter. the input is d(k) where the output will be stored in filter(k). this is my code i hope you can understand what i am trying to do after reading this

%code

T = readtable('1.csv','Format','%f%f%f%f%f%f%f%f');

t=T{:,1};

gFx=T{:,2};

y=T{:,8};

difference=diff(gFx);%calculating difference

d=difference;

[m,n]=size(d);

filter=zeros(m,n);

sum_d=0;

for i=1:length(difference)

if (d(i)& d(i+1)>0)

sum_d=sum_d+d(i)

filter(i)=sum_d

else

sum_d=d(i)

filter(i)=sum_d

end

end

where d= [0 , 0.0107 , 0.0039 ,-0.0019.........upto 761 elements] is a 761x1 dimension array.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_618850

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_618850

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_618861

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_618861

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_619366

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_619366

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_619374

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/422672-how-can-i-accumulate-a-sum-of-positive-differences-in-an-array-by-comparing-all-the-elements#comment_619374

Sign in to comment.