+ Reply to Thread
Results 1 to 20 of 20

counting sign changes

  1. #1
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Question counting sign changes

    Hi,

    I have a set of 5 cells (a row vector), each cell containing a number from -1 to +1. Reading through the values from first to last, I want to count how many times it changes sign (i.e. from positive to negative or vice versa). Zeros are present but need to be ignored (skipped). For example:

    -1 0 1 1 -1 would yield two sign changes (initial value is negative and it switches to positive and then back to negative)

    0 0 1 1 0 would yield zero sign changes (first non-zero value is positive and it remains so)

    -1 0 0 1 0 would yield one sign change

    Obviously I could do this by hand for a small number of vectors, but I need to repeat this calculation across thousands of unique 5 number sets. Although my example above only uses values of -1, 0, +1, I may also need to generalize this such that the numbers may include several different negative and positive values (again, only sign changes are of interest however).

    Any suggestions would be much appreciated. I am working in xl2010.

    Thanks,
    Howard
    Last edited by HRundle; 05-05-2011 at 11:34 AM.

  2. #2
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: counting sign changes

    What about =COUNTIF(A1:E1,"<0")????????
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: counting sign changes

    I was just going to suggest the same but realised that something like -1,-1,-1,1,1 would only be 1 sign change not 3.

    Off into a meeting but will ponder this while I'm staring out of the window (I mean listening intently).

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  4. #4
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    is there any case
    -1 -1 0 1 -1 -> 2 signs?

  5. #5
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    Quote Originally Posted by HRundle View Post
    Hi,

    Any suggestions would be much appreciated. I am working in xl2010.

    Thanks,
    Howard

    If you try to use -1 0 1 , we can :

    + Asuming A1:E1 that contains the data, F1 is result-cell
    + at F2 enter: =SUMPRODUCT(((A1:D1+B1:E1)=-1)+((A1:D1-B1:E1)=2)+((A1:D1-B1:E1)=-2))

    and Copy fill F1 to other result cells
    Last edited by tigertiger; 05-05-2011 at 10:07 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    More exactl and general (for change between negative and positve, or vice versa)
    * Asuming A1:E1 that contains the data, F1 is result-cell
    * at F1; enter the formular
    =SUMPRODUCT(((A1:D1>=0)<>(B1:E1>=0))*1)
    Last edited by tigertiger; 05-05-2011 at 10:11 AM.

  7. #7
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: counting sign changes

    The difficulty is in ignoring the 0's (especially if there is more than one 0 in a row....

    I think the easiest way would be to produce a duplicate table that replaces the 0's with the next 1 in sequence so that the 0 doesn't cause a sign change...

    so if you have a row of values in A1:E1, then say in G1 enter formula:

    =IF(A1=0,IFERROR(INDEX(A1:$E1,MATCH(TRUE,INDEX(A1:$E1<>0,0),0)),F1),A1)

    copied across 5 columns and then down as far as you need to convert all entries.

    Then you can use formula

    =SUMPRODUCT(--(H1:K1<>G1:J1))

    on that new table and copy down.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  8. #8
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Re: counting sign changes

    Quote Originally Posted by tigertiger View Post
    More exactl and general (for change between negative and positve, or vice versa)
    * Asuming A1:E1 that contains the data, F1 is result-cell
    * at F1; enter the formular
    =SUMPRODUCT(((A1:D1>=0)<>(B1:E1>=0))*1)

    This doesn't seem to work in all cases. For example, the following: -1 -1 0 -1 1
    returns a value of 3 while it should be one.

    Howard

  9. #9
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Re: counting sign changes

    Quote Originally Posted by NBVC View Post
    The difficulty is in ignoring the 0's (especially if there is more than one 0 in a row....

    I think the easiest way would be to produce a duplicate table that replaces the 0's with the next 1 in sequence so that the 0 doesn't cause a sign change...

    so if you have a row of values in A1:E1, then say in G1 enter formula:

    =IF(A1=0,IFERROR(INDEX(A1:$E1,MATCH(TRUE,INDEX(A1:$E1<>0,0),0)),F1),A1)

    copied across 5 columns and then down as far as you need to convert all entries.

    Then you can use formula

    =SUMPRODUCT(--(H1:K1<>G1:J1))

    on that new table and copy down.

    That seems to work if all values are -1, 0 or +1, but does not work more generally. For example: {-2 0 -1 1 -1} returns a value of 3 whereas it should be 2. Is there an easy way to replace any negative/positive value in the original table with -1/+1 in the duplicate, which should solve this?

    Thanks!
    Howard

  10. #10
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    Quote Originally Posted by HRundle View Post
    That seems to work if all values are -1, 0 or +1, but does not work more generally. For example: {-2 0 -1 1 -1} returns a value of 3 whereas it should be 2. Is there an easy way to replace any negative/positive value in the original table with -1/+1 in the duplicate, which should solve this?

    Thanks!
    Howard
    why you don't try with option in post #6

    More exactl and general (for change between negative and positve, or vice versa)
    * Asuming A1:E1 that contains the data, F1 is result-cell
    * at F1; enter the formular
    =SUMPRODUCT(((A1:D1>=0)<>(B1:E1>=0))*1)
    .

  11. #11
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: counting sign changes

    I assumed you only had 0, 1, -1 .... Try replacing helper column formula with:

    =SIGN(IF(A1=0,IFERROR(INDEX(A1:$E1,MATCH(TRUE,INDEX(A1:$E1<>0,0),0)),F1),A1))

    copy down and across and use the Sumproduct() formula for final calcs.

  12. #12
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    try at F1 with simple formula:

    =SUMPRODUCT(((A1:D1<=0)<>(B1:E1<=0))*1)

  13. #13
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Re: counting sign changes

    Quote Originally Posted by tigertiger View Post
    why you don't try with option in post #6

    More exactl and general (for change between negative and positve, or vice versa)
    * Asuming A1:E1 that contains the data, F1 is result-cell
    * at F1; enter the formular
    =SUMPRODUCT(((A1:D1>=0)<>(B1:E1>=0))*1)
    .

    Good idea and I've been trying that. However, there is a circular reference problem in the last cell of the formula to create the duplicate table. When you copy

    =IF(A1=0,IFERROR(INDEX(A1:$E1,MATCH(TRUE,INDEX(A1:$E1<>0,0),0)),F1),A1)

    across the 5 columns, if the value in the final column is zero you get a circular reference problem.

    Cheers,
    Howard

  14. #14
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    you should read carefully

    * Asuming A1:E1 that contains the data, F1 is result-cell
    * at F1; enter the formular
    =SUMPRODUCT(((A1:D1<=0)<>(B1:E1<=0))*1)

    you can copy fill for next row F2, F3,....

  15. #15
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,677

    Re: counting sign changes

    Quote Originally Posted by tigertiger View Post
    try at F1 with simple formula:

    =SUMPRODUCT(((A1:D1<=0)<>(B1:E1<=0))*1)
    Does this work, though. If data is, for instance

    0,1,1,1,0 shouldn't the result be 0?
    Audere est facere

  16. #16
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: counting sign changes

    Quote Originally Posted by HRundle View Post
    Good idea and I've been trying that. However, there is a circular reference problem in the last cell of the formula to create the duplicate table. When you copy

    =IF(A1=0,IFERROR(INDEX(A1:$E1,MATCH(TRUE,INDEX(A1:$E1<>0,0),0)),F1),A1)

    across the 5 columns, if the value in the final column is zero you get a circular reference problem.

    Cheers,
    Howard
    I am not sure if you are mixing my suggestion with other people's suggestions...

    See attachment for my suggestion... which works if there is a 0 in last column.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Smile Re: counting sign changes

    Quote Originally Posted by NBVC View Post
    I assumed you only had 0, 1, -1 .... Try replacing helper column formula with:

    =SIGN(IF(A1=0,IFERROR(INDEX(A1:$E1,MATCH(TRUE,INDEX(A1:$E1<>0,0),0)),F1),A1))

    copy down and across and use the Sumproduct() formula for final calcs.

    That seems to work perfectly.

    Many thanks!
    Howard

  18. #18
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Re: counting sign changes

    Quote Originally Posted by NBVC View Post
    I am not sure if you are mixing my suggestion with other people's suggestions...

    See attachment for my suggestion... which works if there is a 0 in last column.
    Indeed I was, but figured it out. You final post confirmed it.

    Thanks again,
    Howard

  19. #19
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: counting sign changes

    Quote Originally Posted by daddylonglegs View Post
    Does this work, though. If data is, for instance

    0,1,1,1,0 shouldn't the result be 0?
    YES it is false
    It is complex than we though

    Hope the one is accurate,

    at F1
    =SUMPRODUCT((A1:D1<>B1:E1)*(A1:D1<>0)*(B1:E1<>0))

  20. #20
    Registered User
    Join Date
    05-03-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    2010
    Posts
    7

    Re: counting sign changes

    Quote Originally Posted by tigertiger View Post
    YES it is false
    It is complex than we though

    Hope the one is accurate,

    at F1
    =SUMPRODUCT((A1:D1<>B1:E1)*(A1:D1<>0)*(B1:E1<>0))
    Thanks, NBVC's solution seems to work perfectly so my problem is solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1