Clear Filters
Clear Filters

Shortening if/elseif loop for value segregation

2 views (last 30 days)
I'm constructing a script that involves locating the centroid of circles and adding their area to matrix that contains the cumulative area of cirlces in 50 pixel intervals (x-direction).
The if loop that I have written works fine but is incredibly long and not easily altered without going through each line. I know that there's a more efficient and easily altered method of doing what I want. Could someone please help me?
Please note that cumulativeData(i,5) is the x-coordinate of the circles centroid and cumulativeData(i,2) is the area of circles.
nRows=size(cumulativeData,1);
cumulativeArea=zeros(25,1);
for i=1:nRows
if (0<cumulativeData(i,5))&&(cumulativeData(i,5)<50)
cumulativeArea(1,1)=cumulativeArea(1,1)+cumulativeData(i,2);
elseif (50<cumulativeData(i,5))&&(cumulativeData(i,5)<100)
cumulativeArea(2,1)=cumulativeArea(2,1)+cumulativeData(i,2);
elseif (100<cumulativeData(i,5))&&(cumulativeData(i,5)<150)
cumulativeArea(3,1)=cumulativeArea(3,1)+cumulativeData(i,2);
elseif (150<cumulativeData(i,5))&&(cumulativeData(i,5)<200)
cumulativeArea(4,1)=cumulativeArea(4,1)+cumulativeData(i,2);
elseif (200<cumulativeData(i,5))&&(cumulativeData(i,5)<250)
cumulativeArea(5,1)=cumulativeArea(5,1)+cumulativeData(i,2);
etc...

Accepted Answer

Stephen23
Stephen23 on 3 Feb 2022
Edited: Stephen23 on 3 Feb 2022
What you are doing can be broken into two parts: the first part is generally known as data binning. You can do data binning quite simply and very efficiently using the HISTCOUNTS command:
Basically you need to feed it a vector of the bin edges (e.g. [0,50,100,...]) and the data that you want to place into those bins. HISTCOUNTS replaces all of your FOR loop and IFs.
Then for the second part you want to sum all of the data together that are within in one bin. There are several approaches you could try, but I would start by using HISTCOUNTS third output argument (the indices) and supply that to ACCUMARRAY, which will give you the sum of the bins.
Lets try it on some fake data, where M is your matrix of data:
M = 200*rand(10,5)
M = 10×5
40.6141 156.9064 169.9576 136.5851 125.9984 159.1910 6.1156 128.5821 8.8958 85.0167 197.7633 33.8743 111.2597 119.3011 180.2383 85.9846 55.4005 19.2142 101.8344 78.3201 154.1138 100.6476 176.2466 91.2217 105.2805 152.4525 44.7698 95.9896 50.3390 98.3560 196.4110 160.4199 171.0495 74.5917 5.4995 102.6071 168.6308 12.3781 57.3968 145.1663 84.8059 82.0437 159.0138 133.0384 78.0269 94.9232 2.8016 32.6950 136.1439 84.5154
V = 0:50:200; % vector of bin edges
[N,~,X] = histcounts(M(:,5),V);
S = accumarray(X,M(:,2),[],@sum) % sum of each bin
S = 4×1
160.4199 191.1312 426.1848 33.8743
  1 Comment
Amere
Amere on 3 Feb 2022
This suggestion works flawlessly. Thank you so much for the solution and explaination. I really appreciate it!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!