How to get Cumulative sum of elements based on negative sign

조회 수: 7 (최근 30일)
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]

채택된 답변

Guillaume
Guillaume 2015년 2월 12일
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
Guillaume
Guillaume 2015년 2월 19일
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.
Raghavendra Reddy P
Raghavendra Reddy P 2015년 2월 23일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by