How Do You Count Opposite Sign Pairs in Mathematica?

  • Context: Mathematica 
  • Thread starter Thread starter ascky
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

This discussion focuses on counting neighbor pairs of opposite signs in Mathematica using the ReplaceList function. The user seeks to identify the number of sign flips in lists containing 1's and -1's. The provided solution utilizes pattern matching syntax to effectively count these pairs, demonstrating that the Length function can be applied to the results of ReplaceList to yield the total count of sign flips. The example outputs confirm the accuracy of the method, showing 1 for the list {1,1,1,-1,-1} and 3 for {1,-1,1,-1,-1}.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of pattern matching in Mathematica
  • Knowledge of the ReplaceList function in Mathematica
  • Basic grasp of list operations and the Length function in Mathematica
NEXT STEPS
  • Explore advanced pattern matching techniques in Mathematica
  • Learn about the use of the Count function in Mathematica for different scenarios
  • Investigate the implications of data manipulation in Mathematica
  • Review additional resources on Mathematica's ReplaceList function and its applications
USEFUL FOR

This discussion is beneficial for Mathematica users, data analysts, and programmers who need to perform list manipulations and count specific patterns within datasets. It is particularly useful for those working with binary data representations.

ascky
Messages
50
Reaction score
0
So I want to be able to count the number of neighbour pairs of opposite sign in a list of 1's and -1's (eg. 1 for {1,1,1,-1,-1} and 3 for {1,-1,1,-1,-1}), but I can't work out the pattern matching syntax in Mathematica even after reading the documentation. I was thinking to use the Count function. Anybody able to help, please?
 
Physics news on Phys.org
Like so many things in Mathematica, I don't know how you would ever stumble onto this on your own.

This will tell you the positions where the sign flip happens.

In[1]:= ReplaceList[{1,1,1,-1,-1}, {pre___, 1,-1,___}|{pre___,-1,1,___} :>Length[{pre}]+1]

Out[1]= {3}

In[2]:= ReplaceList[{1,-1,1,-1,-1}, {pre___,1,-1,___}|{pre___,-1,1,___} :>Length[{pre}]+1]

Out[2]= {1,3,2}

This trick courtesy http://mathematica.stackexchange.com/questions/941/finding-a-subsequence-in-a-list among other places

And if you wrap a Length[] around those it will tell you how many sign flips.

In[3]:= Length[ReplaceList[{1,-1,1,-1,-1}, {pre___,1,-1,___}|{pre___,-1,1,___} :>Length[{pre}]+1]]

Out[3]= 3

When would you think that the way to find how many of something you have is to begin by destroying your data?
MatchQ doesn't work, Position doesn't work, Count doesn't work, not as far as I've ever been able to find.
 
Last edited:
How unintuitive! Thanks for that Bill, I wouldn't have figured it out on my own.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K