Extract values within a range in a for loop

I have a loop like this:
for i = 1:length(x)
xy = find(u==i | u>i & u<(i+1)); #xy should find indices of u within that range i.e u(i)>=i<u(i+1)
N(i) = length(xy) #giving me counts of i
end
but then, the last iteration (last value of i) will be wrong by this expression.
how do i correct this please?
Or any better suggestion (function) by which I can do this
Thanks a lot

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Mär. 2019

0 Stimmen

histcounts or the older histc.
Note that the two functions have different results for values that are exactly equal to the last element of the vector of bins. histc uses a separate bin for values exactly equal to the last bin but histc makes it part of the previous bin.

4 Kommentare

Sesilia Iileka
Sesilia Iileka am 7 Mär. 2019
Thank you, histc did it for me.
One more question please. Is it not possible to create vectors with the values (not just counts) belonging to each bin?
[~, bin] = histc(u, ....);
groups = accumarray(bin(:), u(:), [], @(v) {v});
This would return a cell array in each each entry is the original elements divided up according to the edge information. It is not assumed that the items to go into any one cell are contiguous in the input.
If the items that belong to any one cell are contiguous in the input then there are other approaches available.
Sesilia Iileka
Sesilia Iileka am 8 Mär. 2019
This code has errors Roberson. Please check it again. Thanks
Walter Roberson
Walter Roberson am 8 Mär. 2019
it worked when I tested it using appropriate edge information where I wrote the .... in the outline . however I was confused about which array had the edge information and which had the data to be partitioned.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by