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

조회 수: 1(최근 30일)
Alex Perrakis 2021년 9월 6일
댓글: Walter Roberson 2021년 9월 7일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Jeff Miller 2021년 9월 7일
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표시숨기기 이전 댓글 수: 2
Walter Roberson 2021년 9월 7일
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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Shifting and Sorting Matrices 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!

Translated by