# I would like to find elements of a matrix that fulfill a condition but after every loop the condition is changing.

1 view (last 30 days)
Alex Perrakis on 6 Sep 2021
Commented: Walter Roberson on 7 Sep 2021
Hello,
i have a 428x29 matrix of particle diameters and i would like to sort these diametes in classes beginning from 0.05 to 0.1 μm and ending at 30 μm,so 600 classes, i would like a loop that searchs the matrix and places those elements in another matrix by row. My problem ist that i do not know how i can change the condition after every loop and also how to create a matrix in the form of:
class diameteres
1 ......
2 .....
3
4
.
.
.
I know i might ask a lot but i would love some help because i am stack as all hell!.
Thank you very much
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Jeff Miller on 7 Sep 2021
I think this will almost do what you want.
edges = 0:0.05:30;
[~,~,bin_numbers] = histcounts(diameters,edges);
The output bin_numbers will show, for each diameter, which bin it falls into (what I think you are calling 'class') in your original question. Then, for example, diameters(bin_numbers==1) will be a vector of all of the diameters in the first bin.
In general there might be different numbers of diameters in the different bins, so you might not be able to format the data into a 2d matrix like you show above (i.e., different rows would need different numbers of columns).
##### 3 CommentsShowHide 2 older comments
Walter Roberson on 7 Sep 2021
If you need the count of the number of particles at each size, then
edges = 0:0.05:30;
bin_counts = histcounts(diameters,edges);
and there would be no need to build the cell array that extracts each particle into a bin for that size class.

Sign in to comment.

### Categories

Find more on Numeric Types in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!