同じ条件を10行満たした最初の値を抽出する

10 ビュー (過去 30 日間)
namiyama
namiyama 2022 年 12 月 15 日
編集済み: namiyama 2022 年 12 月 18 日
添付したCSVファイルの内から30以下になる値が連続で20回続く最初の値の位置を特定したいです。
似たような質問がありましたが、応用できず質問しました。
よろしくお願いします。

採用された回答

交感神経優位なあかべぇ
交感神経優位なあかべぇ 2022 年 12 月 15 日
愚直に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
firstIdx = 6
if firstIdx > 0
seriesValues = data(firstIdx + [0 : 19]) % 最初の位置から後続20番目までの値の確認
end
seriesValues = 20×1
9.5613 6.9231 9.3532 4.5835 4.7796 14.5454 25.6322 29.8601 29.1462 25.0001
  1 件のコメント
namiyama
namiyama 2022 年 12 月 16 日
実施できました。分かりやすくありがとうございます。

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

その他の回答 (1 件)

Hernia Baby
Hernia Baby 2022 年 12 月 15 日
編集済み: Hernia Baby 2022 年 12 月 15 日
T = readmatrix('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1231787/test_data.csv');
30以下の数字かつ20行以上続く場合、最初の行番号を抜き出すようにします。
num = myFind(T,30,20,1)
num = 63×1
6 69 128 172 259 367 433 671 701 735
そのときの数字がいくつかは以下のようにすればよいです。
T(num)
ans = 63×1
9.5613 29.6648 28.4386 26.9547 22.3196 28.9476 28.8495 28.7148 23.8581 22.1724
ーーーーーーーーー
関数はこちら
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
  1 件のコメント
namiyama
namiyama 2022 年 12 月 16 日
編集済み: namiyama 2022 年 12 月 18 日
ご丁寧にありがとうございます。コードがまとまりやすかったです。勉強になりました。

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

カテゴリ

Help Center および File ExchangeMATLAB 入門 についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!