同じ条件を10行満たした最初の値を抽出する
2 views (last 30 days)
Show older comments
添付したCSVファイルの内から30以下になる値が連続で20回続く最初の値の位置を特定したいです。
似たような質問がありましたが、応用できず質問しました。
よろしくお願いします。
0 Comments
Accepted Answer
交感神経優位なあかべぇ
on 15 Dec 2022
愚直にfor文を回しました。
csvdata = readtable('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1231787/test_data.csv');
data = csvdata.value;
seriesNum = 0;
firstIdx = -1; % 最初の位置を格納する変数
for idx = 1 : length(data)
if data(idx) <= 30
seriesNum = seriesNum + 1;
if seriesNum >= 20
firstIdx = (idx - 20 + 1)
break;
end
else
seriesNum = 0;
end
end
if firstIdx > 0
seriesValues = data(firstIdx + [0 : 19]) % 最初の位置から後続20番目までの値の確認
end
More Answers (1)
Hernia Baby
on 15 Dec 2022
Edited: Hernia Baby
on 15 Dec 2022
T = readmatrix('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1231787/test_data.csv');
30以下の数字かつ20行以上続く場合、最初の行番号を抜き出すようにします。
num = myFind(T,30,20,1)
そのときの数字がいくつかは以下のようにすればよいです。
T(num)
ーーーーーーーーー
関数はこちら
function num = myFind(T,threshold,n,m)
% threshold:閾値
% n:何行続くか
% m:何行目の番号を取得するか
x = T <= threshold;
a = cell2mat(arrayfun(@(t)1:t,diff(find([1 diff(x(:,1)') 1])),'un',0))';
num = find(a==n) - (n-m);
num = num(x(num));
end
See Also
Categories
Find more on 構造化データと XML ドキュメント in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!