Creating a run log/count with a for loop within a table.

I have a table that has a time column and a binary stability column (1 for stable data, 0 for non). I want to create a run count / run log for the stable data as shown below. The idea is to group stable data into "runs" then breakdown the table and export these runs into separate tables or to identify what range in which a run occurs. The table is rather long ~30000 lines, so I figured a for loop would be best but I am having difficulty identifying the conditional criteria. I appreciate any assistance.

1 件のコメント

dpb
dpb 2020 年 3 月 10 日
Attach the sample data (can make it up, but whole lot easier otherwise... :) )

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

回答 (1 件)

Stijn Haenen
Stijn Haenen 2020 年 3 月 10 日
編集済み: dpb 2020 年 3 月 10 日

0 投票

I think this is what you are looking for:
switch_list=zeros(length(data),1);
switch_list(2:end)=data(2:end,2)-data(1:end-1,2);
up_list=find(switch_list==1);
down_list=find(switch_list==-1)-1;
for i=1:length(up_list)
data(up_list(i):down_list(i),3)=i;
end
I made my own data:
data = [
1 0
2 0
3 0
4 1
5 1
6 0
7 0
8 1
9 0
10 0
11 1
12 1
13 1
14 0
15 0
16 0
17 0
18 1
19 1
20 1
21 0
22 0
23 0
24 1
25 1
26 0
27 1
28 0];

7 件のコメント

Craig Stevens
Craig Stevens 2020 年 3 月 10 日
I already have the output of a 1 or 0. What I am asking for is to create a new column "Run Count' that will identify each grouping of 1's as a run. Please see the screenshot of the example excel sheet. Thanks!
Stijn Haenen
Stijn Haenen 2020 年 3 月 10 日
編集済み: Stijn Haenen 2020 年 3 月 10 日
yes, run my script, change the name data to the name of the matrix with 0 and 1,
I used my own data just as an example. The output of my script will be
1 0 0
2 0 0
3 0 0
4 1 1
5 1 1
6 0 0
7 0 0
8 1 2
9 0 0
10 0 0
11 1 3
12 1 3
13 1 3
14 0 0
15 0 0
16 0 0
17 0 0
18 1 4
19 1 4
20 1 4
21 0 0
22 0 0
23 0 0
24 1 5
25 1 5
26 0 0
27 1 6
28 0 0
Craig Stevens
Craig Stevens 2020 年 3 月 10 日
I swapped "data" for the name of my matrix as shown below:
switch_list=zeros(length(StableOut),1);
switch_list(2:end)=StableOut(2:end,2)-StableOut(1:end-1,2);
up_list=find(switch_list==1);
down_list=find(switch_list==-1)-1;
for i=1:length(up_list)
StableOut(up_list(i):down_list(i),3)=i;
end
I got the errors :
"Error using tabular/length (line 189)
Undefined function 'length' for input arguments of type 'table'. Use the HEIGHT, WIDTH, or SIZE functions instead.
Error in Scanner (line 136)
switch_list=zeros(length(StableOut),1);"
I swapped length for height in your first line of code.
switch_list=zeros(height(StableOut),1);
switch_list(2:end)=StableOut(2:end,2)-StableOut(1:end-1,2);
up_list=find(switch_list==1);
down_list=find(switch_list==-1)-1;
for i=1:length(up_list)
StableOut(up_list(i):down_list(i),3)=i;
end
Now I get the errors:
Undefined function 'minus' for input arguments of type 'table'.
Error in Scanner (line 137)
switch_list(2:end)=StableOut(2:end,2)-StableOut(1:end-1,2);
Thank you for your continued assistance!
Craig Stevens
Craig Stevens 2020 年 3 月 10 日
I then tried to specifically call out the variable for the column of 1's and 0's; as seen below.
switch_list=zeros(height(StableOut),1);
switch_list(2:end)=StableOut.Stable(2:end)-StableOut.Stable(1:end-1);
up_list=find(switch_list==1);
down_list=find(switch_list==-1)-1;
for i=1:length(up_list)
StableOut(up_list(i):down_list(i),3)=i;
end
This appeared to fix that other issue. Now however, an issue appeared for the for loop.
Error using Scanner (line 141)
Right hand side of an assignment into a table must be another table or a cell array.
Stijn Haenen
Stijn Haenen 2020 年 3 月 10 日
you should use a matirx not a table, then it will work.
you can do this with the function: table2array
Craig Stevens
Craig Stevens 2020 年 3 月 10 日
編集済み: Craig Stevens 2020 年 3 月 10 日
I'd prefer a table solution, if possible, due to everything else being in tables. I changed it as you suggessted...
StableOut = table2array(StableOut);
switch_list=zeros(length(StableOut),1);
switch_list(2:end)=StableOut(2:end,2)-StableOut(1:end-1,2);
up_list=find(switch_list==1);
down_list=find(switch_list==-1)-1;
for i=1:length(up_list)
StableOut(up_list(i):down_list(i),3)=i;
end
Recieved the errors:
The following error occurred converting from duration to double:
Undefined function 'double' for input arguments of type 'duration'. To convert from durations to numeric, use the SECONDS, MINUTES,
HOURS, DAYS, or YEARS functions.
Error in Scanner (line 137)
switch_list(2:end)=StableOut(2:end,2)-StableOut(1:end-1,2);
Craig Stevens
Craig Stevens 2020 年 3 月 10 日
I appreciate all the help. I was able to accomplish what I wanted through a series of if statements within a for loop. Thank you again for your assistance.

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

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2020 年 3 月 10 日

コメント済み:

2020 年 3 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by