How to create a new column that depends on the stabilization of another

1 回表示 (過去 30 日間)
Margarida
Margarida 2023 年 3 月 2 日
コメント済み: Margarida 2023 年 3 月 2 日
The title is pretty vague so here's my example:
I have a column on a table whose values are true and false (1 for when it's on and 0 for off)
Table.On = [0;0;0;1;1;1;1;1;0;0;1;1;1;1;0;1;1] (and so on)
However, I need a column that will provide stable values, since the first or second or third consecutive 1's, it's still "heating up", so it's only stable on the forth 1.
So I would like to create a new column like this (if it's 0 again it resets the same procedure):
Table.Stable = [0;0;0;0;0;0;1;1;0;0;0;0;0;1;0;0;0] ---- only consecutive 1's after 3 consecutive 1's.
This could be easily done in a for loop or while but I have 90000 rows and thats would take a long time. Is there a way to do this without a for/while loop?
Thank you!!

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 2 日
Using find() will be slower than using logical indexing, specially given the number of rows you have
in = [0;0;0;1;1;1;1;1;0;0;1;1;1;1;0;1;1];
out = movsum(in,[3 0])==4
out = 17×1 logical array
0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0

その他の回答 (1 件)

Cameron
Cameron 2023 年 3 月 2 日
Table.On = [0;0;0;1;1;1;1;1;0;0;1;1;1;1;0;1;1];
indx = find(movsum(Table.On,4) == 4) + 1;
Table.Stable = zeros(length(Table.On),1);
Table.Stable(indx) = 1;
  2 件のコメント
Margarida
Margarida 2023 年 3 月 2 日
ahhhh! so cool! thank you so much :)
Cameron
Cameron 2023 年 3 月 2 日
@Dyuman Joshi is correct. Using find() is less efficient so use this instead.
Table.On = [0;0;0;1;1;1;1;1;0;0;1;1;1;1;0;1;1];
Table.Stable = movsum(Table.On,[3 0]) == 4;

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by