How to get the first value in a set of array that is bigger than the first few values?

1 回表示 (過去 30 日間)
Hi everyone,
I have two arrays:
y = [1 1 1 1 1 1.5 1.7 1.9 2 2.7]
x = [25:35]
How can I find the corresponding x value for the first number that breaks the chain in y (in this case 1.5)?
y values are different for each loop but the x is almost stable.
Furthermore, how can I store the x y values of the first value that breaks the chain in a loop. Thank you so much.

採用された回答

Star Strider
Star Strider 2021 年 2 月 21 日
編集済み: Star Strider 2021 年 2 月 21 日
Try this:
y = [1 1 1 1 1 1.5 1.7 1.9 2 2.7];
x = [25:35];
TF = ischange(y,'variance');
Idx = find(TF,1,'first')
Out = x(Idx)
producing:
Out =
30
I’m not certain what you want, or how robust this would be to your other data, so you may need to experiment with it.
  4 件のコメント
Wolfgang McCormack
Wolfgang McCormack 2021 年 2 月 22 日
@Star Strider Thank you so much!, I have one more question, Now, how can I calculate the slope of the line between the first and last? and how can I store the original files of them in two different folders when the slope is greater than A?
I really appreciate it! sorry for asking too many questions :D
Star Strider
Star Strider 2021 年 2 月 22 日
As always, my pleasure!
Now, how can I calculate the slope of the line between the first and last?
Please define ‘first and last’.
Also, for ths slope calculation, do you want to consider only those two points, or all the points in between as well? The first involves a simple calculation of only those two points, and the second involves a least-squares linear fit. (Both of these are straightforward in MATLAB, so that is not the issue. I just need to know which of these you want.) What data do you want the slope to be calculated with respect to? I assume this is essentially with ‘x’ and ‘y’ defined as previously.
‘... and how can I store the original files of them in two different folders when the slope is greater than A?
Do you want to store one set in a folder when the slope is less than or equal to ‘A’ and in another folder when it is greater than ‘A’, or something else? Do the folders already exist or will it be necessary to create them?

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2021 年 2 月 21 日
Because x and y are not the same length, I'm not sure how to make the "correspondence" between them. However, this code snippet will give the index of the first element that is different from the preceding ones:
idx = find(diff(y)~=0,1) + 1;
Maybe you can figure out the rest from there, or explain more about what you mean.
  3 件のコメント
the cyclist
the cyclist 2021 年 2 月 21 日
I'm not sure what you are asking. If your input vector is
y = [7 7 7 3 4];
then
idx = find(diff(y)~=0,1) + 1
idx = 4
gives 4 as the result, because it is the first element that "breaks the chain" of the initial values. Is that what you mean?
Wolfgang McCormack
Wolfgang McCormack 2021 年 2 月 22 日
@the cyclist oh I see thank you! Can I accept two answers?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by