MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。


Question about array alternating between positive and negative

William Fantin さんによって質問されました 2019 年 5 月 15 日
最新アクティビティ James Tursa
さんによって コメントされました 2019 年 5 月 15 日
I have solved a differential equation symbolically and then evaulated it based on user input. The result is different every time. Each time I obtain an array from the graph. For example, lets say I get an array, a=[-1,1,2,-2,1,5,-4,1]. Is there a way to find out how many times the sign of an array changes from positive to negative? The reason I am trying to do this is I need to know how many times the curve crosses the origin.

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: James Tursa
2019 年 5 月 15 日
編集済み: James Tursa
2019 年 5 月 15 日

Seems like there should be a shorter way, but assuming you are looking for all 0 crossings and not just the positive to negative ones:
This will double book the exact 0's, however. So if you expect you might get those, you will need something more sophisticated.

  2 件のコメント

IT WORKED!!!! THANK YOU!!! i had sunusoidal motion about the x-axis and it crossed 6 times and your method gave me 6! what did you mean it wont exactly count zeros correctly?
THanks again
>> a = [-2 -3 0 1 2 -3 5]
a =
-2 -3 0 1 2 -3 5
>> sum(logical(diff(sign(a))))
ans =
There's really only three 0 crossings, but the calculation above gave a result of 4. The 0 crossing from -3 --> 0 --> 1 got double booked because the sign changed twice even though it was only one crossing. So if you expect you might get exact 0's you will need a more sophisticated method to detect them and check for 0 crossings vs 0 peaks etc and adjust the answer accordingly.

サインイン to comment.

Translated by