Data set must contain at least 3 samples error in find peaks embedded in for loop

9 次查看(过去 30 天)
Hi All,
I am trying to iterate through the columns of a matrix I have called 'data' to find the peaks of each column. To do this, I have a for loop that essentially looks like this:
for i = 1:size(data,2);
[pks{i,:},locs{i,:}] = findpeaks(bcgFixed(i,:),'MinPeakProminence',(bcgthresh*bcgStd),'MinPeakDistance', ((6/frate)* 10^-3)); %find all points where the baseline-subtracted background trace crosses 2*std
numEvents = length(locs)
% disp(locs)
% figure
% findpeaks(bcgFixed(i,:),'MinPeakProminence',(bcgthresh*bcgStd),'MinPeakDistance', ((1/frate)* 10^-3));
end
when I try to run the code it gives me the error message in the title. I'm not sure why because this matrix contains a lot more than 3 samples. Would really appreciate some help with this one.

采纳的回答

Star Strider
Star Strider 2019-11-10
... columns of a matrix ...
Note that:
[pks{i,:},locs{i,:}] = findpeaks(bcgFixed(i,:),'MinPeakProminence',(bcgthresh*bcgStd),'MinPeakDistance', ((6/frate)* 10^-3)); %find all points where the baseline-subtracted background trace crosses 2*std
will iterate across rows.
To iterate across columns, use:
bcgFixed(:,i)
instead:
[pks{i,:},locs{i,:}] = findpeaks(bcgFixed(:,i),'MinPeakProminence',(bcgthresh*bcgStd),'MinPeakDistance', ((6/frate)*1E-3)); %find all points where the baseline-subtracted background trace crosses 2*std
The cell arrays storing the output do not care, however the input array does.
  12 个评论
Christiane Voufo
Christiane Voufo 2019-11-10
Could I get help on this last one? The error says that the matrices I'm trying to subtract are not equal. They both have the same size, but column 1 in matrix bcgMean is all zeros and I think this is the problem, althouhg I'm not sure why the first row of this matrix is not getting populated in the first for loop. I'm not sure if this is even the problem.
bcgMean = zeros(1,577);
bcgMean1 = zeros(1750,577);
bcgFixed = zeros(1750,577);
for i2 = 2:size(valarray,2)
bcgMean(i2) = mean(valarray(:,i2));%find mean
bcgMean1 = repelem(bcgMean(1:1750:end,:),1750,1);
end
for i = 2:size(valarray,2)
bcgFixed(i) = (valarray(:,i) - bcgMean1(:,i2)); %subtract mean to center at zero
end
Star Strider
Star Strider 2019-11-10
I apologise for the delay — sleeping.
I cannot understand what you are doing, especially with:
bcgMean1 = repelem(bcgMean(1:1750:end,:),1750,1);
Note that you can calculate the row means of ‘valarray’ as:
bcgMean = mean(valarray);
so including it in the loop is unnecessary.
Here you are using the last column (whatever the last value of ‘i2’ was in the previous loop) of ‘bcgMean1’:
bcgFixed(i) = (valarray(:,i) - bcgMean1(:,i2)); %subtract mean to center at zero
from all of ‘valarray’, column-wise. If they do not have the same row sizes, that would throw the error you are seeing.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by