cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

How to count sign changes in a vector/matrix and at what positions is there a sign change?

Cornel
18-Opal

How to count sign changes in a vector/matrix and at what positions is there a sign change?

Hello,

Cornel_0-1695739655162.png

Cornel_1-1695739661242.png

Cornel_2-1695739671791.png

 

1. I would like to know how many sign changes there are in this vector viewed as a matrix.

There are 8 sign changes as far as we can see:

Cornel_3-1695739730688.png

So, here I would like a function that shows me 8 as output (the number 8 being the number of sign changes).

2. I would like a function that tells me that at column 0 and between lines 1 and 2 is a change of sign, that at column 0 and between lines 2 and 3 there is a change of sign, etc. :

Cornel_4-1695740199280.png

1 ACCEPTED SOLUTION

Accepted Solutions

Here are Lucs functions modified for option 1 or option 2 as explained above. I would vote for option 1 (ignoring the zeros)

Werner_E_0-1695769295118.png

 

View solution in original post

13 REPLIES 13
LucMeekes
23-Emerald III
(To:Cornel)

Here you go:

LucMeekes_0-1695750683768.png

LucMeekes_1-1695750691529.png

Note that you have to supply the argument to the functions as an array, a vector to be specific. I abused an undocumented trick in the attached file: added an evaluation (=) to the end of the definition of the range.

 

Success!

Luc

 

@Werner_E 

 

How to think in the case of complex numbers? Does have sense in the case of complex numbers to view sign changes? Or is not possible for complex numbers this thing? Can be included also complex numbers in this counting of sign changes and locations?

Cornel_0-1695799361563.png

Cornel_1-1695799374662.png

Cornel_2-1695799386412.png

LucMeekes
23-Emerald III
(To:Cornel)

There are three 'sign' functions: sign(), signum() and csgn(). Look them up in help.

Success!
Luc

Is it possible to skip the complex numbers in SignChanges and ChangeLocations functions if vector has both real numbers and complex numbers, and maintain in the counting only the real numbers?

Werner_E
24-Ruby V
(To:Cornel)


@Cornel wrote:

Is it possible to skip the complex numbers in SignChanges and ChangeLocations functions if vector has both real numbers and complex numbers, and maintain in the counting only the real numbers?


Sure. Look at my ChangeLocations1() function where I trimmed the zeros and kept the original index positions. You can do similar by trimming the numbers with a non-zero  imaginary part.
Adding this should do the job

Werner_E_0-1695815519982.png

 

Werner_E
24-Ruby V
(To:Cornel)


Does have sense in the case of complex numbers to view sign changes?

Does it makes sense to count the sin changes in a series of real numbers?
YOU asked for a function to to do, so YOU should know how it makes sense for you and what for you would need that information.

According complex numbers - you would first have to clearly define what you would consider a "sign change" in the case of complex numbers.


@Werner_E wrote:
According complex numbers - you would first have to clearly define what you would consider a "sign change" in the case of complex numbers.

So, for the complex numbers I would consider a sign change in these 2 cases:

1.  Re(z) will stay positive and Im(z) will change sign:

Cornel_1-1695816892930.png

Cornel_2-1695816900992.png

Cornel_3-1695816912057.png

 

or

 

2.  Re(z) will stay negative and Im(z) will change sign:

Cornel_4-1695816967962.png

Cornel_5-1695817071237.png

Cornel_7-1695817127286.png

 

EDIT: But hold on a little with this complex numbers. I would need to think a little about these complex numbers, if it helps somehow or not. I will post if needed.

Werner_E
24-Ruby V
(To:Cornel)

How would you deal with a zero in the vector. Needs definition!

Lets say the vector is (3, 1, 0, 0, 0, -4. -5, 0, 0, -3)^T. How many sign changes and at which positions?

Werner_E_0-1695765953359.png

Basically I see two options:

 

Option 1:  If we treat zero as indifferent, neither pos nor neg, then I would say we have just one sign change (ignoring all zeros), but I could not say at which position - somewhere around 2,3,4? Maybe we say position 1 as being the last positive value?

 

Option 2:  If we treat zero as a third case of sign, then we would have 4 sign changes. probably at 1,4,6 and 8.

 

As usual in math its just a matter of definition 😉

 

Here are Lucs functions modified for option 1 or option 2 as explained above. I would vote for option 1 (ignoring the zeros)

Werner_E_0-1695769295118.png

 

So, yes, we can treat all zeros as indifferent, and we can take the last positive/negative value for position.

Cornel_0-1695810343259.png

 

Cornel_5-1695810633723.png

 

Cornel_2-1695810404278.png

Cornel_3-1695810410505.png

So, yes, these (SignChanges1 and ChangeLocations1) seems better.

 

And for this, could be possible something like this for ChangeLocations1:

Cornel_7-1695810947207.png?

 

 

@LucMeekes 

Is it possible to do like that?

Cornel_3-1695809426149.png

Cornel_4-1695809432656.png

 

Cornel_8-1695811114523.png

Werner_E
24-Ruby V
(To:Cornel)

Simply change the last assignment in my ChangeLocations1()

Werner_E_2-1695815842582.png

 

Werner_E_3-1695815969046.png
Werner_E_4-1695815977791.png

Werner_E_5-1695815990301.png

Werner_E_6-1695816043226.png
Werner_E_7-1695816050617.png

 

 

 

LucMeekes
23-Emerald III
(To:Cornel)

See if this helps:

LucMeekes_0-1695815795030.png

LucMeekes_1-1695815804824.png

LucMeekes_2-1695815826289.png

It doesn't see the sign change from pos 1 to 2, but from pos 4 to 5 instead.

Remembering that you had a positive value 1 at pos 1 when you finally see a negative value -4 at pos 5 is possible, but more cumbersome.

And for a case with complex numbers:

LucMeekes_3-1695816196336.png

LucMeekes_4-1695816206427.png

LucMeekes_5-1695816216937.png

Success!
Luc

 

 

In the last example, shouldn't the result be vector (0; 5) and not (4;5) ?

EDIT: Ah, sorry, I missed the fact that you had already addressed this issue yourself

Top Tags