# 【matlab】动态数组的平均值问题

``````a=csvread('C:\Users\Administrator\Desktop\ppgsource\BVP.csv');
rel_data=a(50624:53568);
Fs=64;
t=[];
rel_data=filter(ditong3,rel_data);
rel_data=filter(bandpass_hann,rel_data);
n=length(rel_data);
for i=1:n
t(i)=i/Fs;
end
wins.t_start=0;
wins.t_end=n;
t=t';
%rel_data=[t;rel_data];
%field={'t','v'};
data=struct('t',t,'v',data);
rel_data=struct('t',t,'v',rel_data);
%rr.t = mean([wins.t_start(:)' ; wins.t_end(:)']);
rr.t = t(:);
rr.v = nan(length(rr.t),1);
for win_no = 1 : length(wins.t_start:wins.t_end)

%% extract relevant data
rel_els = find(rel_data.t >= wins.t_start& rel_data.t < wins.t_end);
data.v = rel_data.v(rel_els);
data.t = rel_data.t(rel_els);

%% eliminate nans
good_els = ~isnan(data.v);
data.v = data.v(good_els); data.v = data.v(:);
data.t = data.t(good_els); data.t = data.t(:);

%% identify peaks
diffs_on_left_of_pt = diff(data.v);diffs_on_left_of_pt = diffs_on_left_of_pt(1:(end-1)); diffs_on_left_of_pt = logical(diffs_on_left_of_pt>0);
diffs_on_right_of_pt = diff(data.v);diffs_on_right_of_pt = diffs_on_right_of_pt(2:end); diffs_on_right_of_pt = logical(diffs_on_right_of_pt<0);
peaks = find(diffs_on_left_of_pt & diffs_on_right_of_pt)+1;

%% identify troughs
diffs_on_left_of_pt = diff(data.v); diffs_on_left_of_pt = diffs_on_left_of_pt(1:(end-1)); diffs_on_left_of_pt = logical(diffs_on_left_of_pt<0);
diffs_on_right_of_pt = diff(data.v); diffs_on_right_of_pt = diffs_on_right_of_pt(2:end); diffs_on_right_of_pt = logical(diffs_on_right_of_pt>0);
troughs = find(diffs_on_left_of_pt & diffs_on_right_of_pt)+1;

%% define threshold
q3 = quantile(data.v(peaks), 0.5);
thresh = 0.2*q3;

%% find relevant peaks and troughs
extrema = sort([peaks(:); troughs(:)]);
rel_peaks = peaks(data.v(peaks) > thresh);
rel_troughs = troughs(data.v(troughs) < 0);
%% find valid breathing cycles
valid_cycles = zeros(length(rel_peaks)-1,1);
cycle_durations = nan(length(rel_peaks)-1,1);
for peak_no = 1 : (length(rel_peaks)-1)

% valid if there is only one rel_trough between this peak and the
% next
cycle_rel_troughs = rel_troughs(rel_troughs > rel_peaks(peak_no) & rel_troughs < rel_peaks(peak_no+1));
if length(cycle_rel_troughs) == 1
valid_cycles(peak_no) = 1;
cycle_durations(peak_no) = data.t(rel_peaks(peak_no+1)) - data.t(rel_peaks(peak_no));
end

end

if sum(valid_cycles) == 0
rr.v(win_no) = nan;
continue
end
%% Calc RR
% Using average breath length
ave_breath_duration= nanmean(cycle_durations);
rr.v(win_no) = 60/ave_breath_duration;
end
``````
• 写回答

#### 2条回答默认 最新

• BitGPT 2023-03-05 09:53
关注

该回答引用ChatGPT

问题出在以下代码段：

``````if sum(valid_cycles) == 0
rr.v(win_no) = nan;
continue
end

``````

当没有找到任何有效呼吸周期时，代码会将该窗口的RR值设置为NaN并继续执行循环。但是，由于没有找到有效呼吸周期，因此平均呼吸持续时间“ave_breath_duration”仍然保持NaN值。然后，对NaN值执行除法运算会导致RR值也变为NaN值。因此，无论数组长度为多少，得到的平均值始终不变。

要解决这个问题，可以在“if”语句中添加代码以设置“ave_breath_duration”的默认值。例如，将以下代码添加到“if”语句之前：

``````ave_breath_duration = NaN;

``````

这将确保“ave_breath_duration”始终具有值，并且除法运算可以正确执行。

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 3月13日
• 已采纳回答 3月5日
• 创建了问题 3月5日

#### 悬赏问题

• ¥15 matlab中此类型的变量不支持使用点进行索引
• ¥15 咨询第六届工业互联网数据创新大赛原始数据
• ¥15 Pycharm无法自动补全，识别第三方库函数接收的参数！
• ¥15 STM32U575 pwm和DMA输出的波形少一段
• ¥30 android百度地图SDK海量点显示标题
• ¥15 windows导入environment.yml运行conda env create -f environment_win.yml命令报错
• ¥15 这段代码可以正常运行，打包后无法执行，在执行for内容之前一直不断弹窗，请修改调整
• ¥15 C语言判断有向图是否存在环路
• ¥15 请问4.11到4.18以及4.27和4.29公式的具体推导过程是怎样的呢
• ¥20 将resnet50中的卷积替换微ODConv动态卷积