How can I select all parts of a vector?

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)

Remove the -1 from your for loop.
for ci = 1:numel(bins)-1

9 Comments

did you run the code?
i get this error when i remove the -1
Index exceeds the number of array elements (64).
Error in DETECCIONONEUSER (line 13)
ind = (ceil(max(bins(ci),1)):floor(bins(ci+1)));
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.
hello dpb, can you help me? with that of the floor so that it can reach the last point?
if numel(ind_val_ab_th)>=1 % this comment is outdated hehe
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...
hello dpb is a very good logic, but what happens is that in that variable ind
ind = (ceil(max(bins(ci),1)):floor(bins(ci+1)));
I not only calculate the range, I also calculate the exact point where the highest peak is within that BIN. once you calculate at what point is the highest peak on this line
[M,I] = max(data);
It calculates the true point of the entire vector, for example inside the bin there are 24 parts, the highest peak can be at point 13, on this line
xmax = ind(I);
I get the true point of the whole vector, in this case it would have to be 1520.
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... :)
I did not understand, in what line of code?
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?
the code continued without finding the last one, but I modified a line and it worked perfectly, how can I accept your question? you answered another user's question hehe

Sign in to comment.

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!