How to get Cumulative sum of elements based on negative sign

I have 2 vectors >> z=1:9; >> l=[1 2 -3 4 -5 6 7-8 9 10] starting from last element of l i.e 10 9....1 check for 1st negative element i.e. l=-8 if l=-8 add z8+z9 check for next negative element i.e l=-5 if l=-5 add z5+z6+z7 (elements between two negative numbers -5 and -8) check for next negative element i.e l=-3 add z3+z4 and add z1+z2 finally i should get new vector w=[z1+z2, z3+z4,z5+z6+z7,z8+z9]

 Accepted Answer

Your array l has one more element than z. why? Anyway,
z = 1:9;
l = [1 2 -3 4 -5 6 7 -8 9];
negposition = find(l < 0);
w = arrayfun(@(s, e) sum(z(s:e)), [1 negposition], [negposition-1 numel(z)])

9 Comments

Thank you..This is the great response.If it works without loops its really cool. yes sir, array L has 'n'elements and array z has 'b=n-1'. in my problem n implies no. of nodes and b implies branches which equals n-1. sir could you please elaborate how this coding of w= .... works. i have checked placing negative sign at diff. location n its working exactly the way i wanted. thank you
Could you please go ahead and mark the answer as accepted so that Guillaume will earn reputation points and ear privileges in this forum? You can also vote for it too, like I did.
Raghavendra,
The arrayfun is equivalent to the following:
fn = @(s, e) sum(z(s:e)); %1st argument of arrayfun, an anonymous function
arr1 = [1 negposition]; %2nd argument of arrayfun
arr2 = [negposition-1 numel(z)]; %3rd argument of arrayfun
assert(size(arr1) == size(arr2)); %arrayfun makes sure the array sizes match
w = zeros(size(arr1)); %return value of arrayfun
for idx = 1:numel(arr1) %arrayfun iterates over the arrays
w(idx) = fn(arr1(idx), arr2(idx));
end
As you can see, it's much easier to use arrayfun.
Note that the anonymous function is kind of equivalent to:
function out = fn(s, e)
out = sum(z(s, e));
end
except that a separate function would not have access to z.
Thank you sir. if array l is complex vector, could it possible to consider if negative imaginary part of the element as Negative element..??? suppose l =
[0.0000 + 0.0000i
0.6000 + 0.6000i
0.4000 - 0.3000i
1.5500 + 0.5500i
-0.7000 + 0.7000i
2.0000 + 0.1500i
0.5500 + 0.5500i
0.4500 - 0.4500i
1.4000 + 0.4000i
-0.6500 + 0.0000i
0.4000 + 0.3000i
1.5000 + 0.1500i]
in above veoctor l(3)= 0.4000 - 0.3000i right i wanted to consider this element as negative(-L3) in my calculation, because imaginary part is negative. overall, whether negative sign is on real part or imag part, i wanted to treat it as negative element. hence in above mentioned array, negative elements are [-L3(imag is negtiv), -L5(real is neg), -L8(img is neg), -L10(real is neg)]
this should continue calculation for finding cumsum between elements based on negative elements like earlier steps you mentioned.
To match whichever condition you want, the only thing you need to change is the test inside the find. To match numbers with either real or imaginery negative value:
negposition = find(real(l) <0 | imag(l) < 0);
Thank you sir, am really happy with your answers.

How can i arrive the solution when am having more than one 'end' node i.e. system contains main feeder and laterals. please find the attached images for your reference.

When there is single feeder, your coding works absolutely fine. i faced some problem when there are laterals. for example, Vector L has 4-end elements L10(main-feeder) and L13, L21,L18(on laterals). Final answer should consider this laterals too.

>> L=[1 2 -3 4 5 -6 -7 8 9 10 11 12 13 14 -15 16 17 18 19 -20 21]; >> Z=1:20;

I don't see how you differentiate between your laterals and feeders in L.
In any case, I would suggest you start a new question. You'll get more people looking at it as when a question has an accepted answer, people don't look at them anymore.
You rightly pointed out. Yes sir, we cannot differentiate main feeder and laterals based on the values of Vector L. We can differentiate by looking at vector Z. This how my actual Z vector looks like %% LINE NO FROM TO Real Imaginary Z= [1 1 2 0.0058 0.0029 2 2 3 0.0308 0.0157 3 3 4 0.0228 0.0116 4 4 5 0.0238 0.0121 5 5 6 0.0511 0.0441 6 6 7 0.0117 0.0386 7 7 8 0.1068 0.0771 8 8 9 0.0643 0.0462 9 9 10 0.0651 0.0462 10 3 11 0.0123 0.0041 11 11 12 0.0234 0.0077 12 12 13 0.0916 0.0721 13 6 14 0.0338 0.0445 14 14 15 0.0369 0.0328 15 15 16 0.0466 0.0340 16 16 17 0.0804 0.1074 17 17 18 0.0457 0.0358 18 8 19 0.0102 0.0098 19 19 20 0.0939 0.0846 20 20 21 0.0255 0.0298
In above Z vector, 1st column indicates line no(or branch no.), 2nd and 3rd column indicates from where to where line connected, 3rd and 4th column indicates actual real n img values to be summed based on sign of vector L.
Red marking indicates laterals starting point i.e for ex: line no 10, connected between node 3 and 11. Node 3 already connected to main feeder node 4 i.e. 3-4-5……-10 Also node 3 connected to lateral i.e 3-11-12-13(end point)
Similarly, Node 6 has lateral 6-14-15-16-17-18 and node 8 has lateral 8-19-20-21. We can differentiate main feeder n lateral by looking at From n To column of Z.
My vector L looks like %%% Real Imag L = [ 0.0000 0.0000 0.0100 0.0060 -0.0090 0.0040 0.0120 0.0080 0.0060 0.0030 0.0060 -0.0020 -0.0200 0.0100 0.0200 -0.0100 0.0060 0.0020 0.0060 0.0020 0.0045 0.0030 0.0060 0.0035 0.0060 0.0035 0.0120 0.0080 -0.0060 0.0010 0.0060 0.0020 0.0060 0.0020 0.0090 0.0040 0.0090 0.0040 0.0090 -0.0040 0.0090 0.0040]
As per your suggestion, I will create fresh question with all necessary details. Thank you sir.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!