連続値の抽出

38 次查看(过去 30 天)
toshi shwa
toshi shwa 2022-2-3
添付のようなCSVファイルがあります。
signal列において、NAを含むことなく数字が5回、10回連続する際の初めの#列の数値を抽出したいです。
例1:5回連続、#列 8
例2:10回連続、#列 25

采纳的回答

Atsushi Ueno
Atsushi Ueno 2022-2-3
编辑:Atsushi Ueno 2022-2-3
mat = readmatrix('例題.csv')'; % 転置して読み込み
len = 0;
for k = ~isnan(mat(1,:))
len(end+1) = (len(end)+1)*k; % 連長圧縮の応用(lenはmatより1列長くなる事に注意)
end
mat(2, find(len==5,1)-5) % 例1:signal列において、NAを含むことなく数字が5回連続する際の初めの#列の数値
ans = 8
mat(2, find(len==10,1)-10) % 例2:signal列において、NAを含むことなく数字が10回連続する際の初めの#列の数値
ans = 25
[mat; len(2:end)] % 【参考】1行目:signal列の転置、2行目:#列の転置、3行目:連続する数値列の長さ
ans = 3×100
NaN NaN 2 1 2 1 NaN 1 2 2 3 2 2 1 2 NaN 2 NaN 1 1 1 1 1 NaN 1 2 3 4 1 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 0 1 2 3 4 0 1 2 3 4 5 6 7 8 0 1 0 1 2 3 4 5 0 1 2 3 4 5 6
  1 个评论
toshi shwa
toshi shwa 2022-2-4
うまくいきました!ありがとうございます。

请先登录,再进行评论。

更多回答(1 个)

Hernia Baby
Hernia Baby 2022-2-3
こちら応用しました。
Sample = readtable('例題.csv');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
n = 5;
m=1;
a = ismissing(Sample(:,1));
5回以上連続する行
idx5 = myFind(a,5,1)'
idx5 = 1×6
8 19 25 43 63 80
10回以上連続する行
idx10 = myFind(a,10,1)'
idx10 = 1×4
25 43 63 80
関数は以下のようになります。
function idx = myFind(a,n,m)
x = cell2mat(arrayfun(@(t)1:t,diff(find([1 diff(a(:,1)') 1])),'un',0))';
idx = find(x==n) - (n-1);
end

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!