How can I select all parts of a vector?
Show older comments
hello everyone, I have a problem with my code that I cannot solve, I have a vector of 1536, what I am trying to do is select a part for each iteration for, that is, basically I divide that vector of 1536 into 64 parts of 24, and in each part of those 24 I try to discover the peak that crosses a threshold, the problem is that I only get to point 1512 of the 1536 and there is a last peak that crosses the threshold at point 1520 as shown in the image, can someone help me please?
Thank you in advance.
attached bin2.mat and pdp.mat

Answers (1)
Mitch Lautigar
on 11 May 2022
0 votes
Remove the -1 from your for loop.
for ci = 1:numel(bins)-1
9 Comments
giancarlo maldonado cardenas
on 11 May 2022
dpb
on 11 May 2022
This is probably an ideal case in which to use the debugger to track down the flaw in your logic (or, perhaps, the input bin data may not be what should be).
I'm guessing that the problem is related to the use of floor in the line
ind = (ceil(max(bins(ci),1)):floor(bins(ci+1)));
isn't giving you the upper limit you think it should.
I also note there's a disparity between the comment and the code in the line
if numel(ind_val_ab_th)>=1 % we must have at least two values in one bin
The condition will be satisfied for one element by having the ">=" instead of ">" only. Whether this is a problem with the code or simply the comment is out of date I don't know, but looks worthy of checking.
giancarlo maldonado cardenas
on 11 May 2022
Have you shown that is the problem? If it's a problem on the last group, it's also possibly an issue on the others.
If you're binning in even chunks as is, then there's no need for any rounding at all in the indices, simply use 1:24. 25:48, ... for each section.
NperG=24; % set your group size
NG=size(pdp,1)/NperG; % compute number of groups
i1=1; % the first index
for i=1:NG-1 % iterate over groups
i2=i1+NperG-1; % end of this group
data=pdp(i1:i2); % get that data set (NB: can use pdp(i1:i2) directly w/ no copy needed)
...do whatever here with the subsection...
i1=i2+1; % increment i1 to next point after end of this group, rinse and repeat
end
If you want to continue with the bins idea, use discretize and the returned bin numbers and will be inclusive.
As far as the overall idea, if you have the Signal Processing TB, findpeaks has all this stuff builtin to return the peaks with all kinds of discrimation available; simply return the peaks and count those within a given binning would do it all...
giancarlo maldonado cardenas
on 11 May 2022
dpb
on 11 May 2022
So, you just add the beginning index to the point you find inside the bin (with a -1 correction since MATLAB arrays count from 1).
No problem...it's done all the time... :)
giancarlo maldonado cardenas
on 11 May 2022
Wherever you find the index inside the binned subset, the overall index to the original array is simply that index plus the starting bin index.
For example, in
[M,I] = max(data);
I will be the location of the maximum in the subsection counting from 1; so the same value/location in the orginal vector will be the location
Ibig=i1+I-1;
Isn't that pretty self-evident since you're beginning each data segment at postion i1 in the full vector?
giancarlo maldonado cardenas
on 13 May 2022
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!