How to Call first and last element in a Matrix of changing size in a Loop
3 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have a data set that needs to be discretized with various numbers of bins. I have acheived this using the discretize() function. My goal is to recreate a Cumulative Distribution Function for a full data set with a smaller number of points.
% Normalized Distribution Data
PerVolsoft = [
17.9116652495797
35.8233304991566
35.8233304991566
53.7349957487335
71.6466609983134
71.6466609983134
71.6466609983134
107.469991497467
107.469991497467
107.469991497467
125.381656747047
143.293321996624
161.204987246201
161.204987246201
179.116652495779
197.028317745356
250.763313494090
268.674978743668
268.674978743668
304.498309242824
304.498309242824
322.409974492402
322.409974492402
322.409974492402
322.409974492402
322.409974492402
322.409974492402
322.409974492402
322.409974492402
286.586643993247
286.586643993247
286.586643993247
286.586643993247
286.586643993247
286.586643993247
286.586643993247
286.586643993247
286.586643993247
286.586643993247
304.498309242824
304.498309242824
304.498309242824
322.409974492402
358.233304991558
411.968300740292
465.703296489024
555.261622736914
644.819948984803
770.201605731850
913.494927728471
1056.78824972509
1200.08157172172
1325.46322846877
1450.84488521581
1612.04987246201
1755.34319445862
1862.81318595609
2024.01817320229
2167.31149519891
2292.69315194596
2435.98647394258
2579.27979593921
2722.57311793583
2865.86643993245
3009.15976192907
3134.54141867612
3277.83474067274
3421.12806266936
3582.33304991558
3725.62637191220
3851.00802865925
3994.30135065587
4119.68300740292
4262.97632939954
4424.18131664574
4567.47463864236
4657.03296489024
4567.47463864236
4424.18131664574
4298.79965989869
4173.41800315165
4012.21301590545
3886.83135915840
3743.53803716178
3582.33304991558
3474.86305841811
3331.56973642149
3170.36474917529
3044.98309242824
2901.68977043162
2776.30811368457
2776.30811368457
2847.95477468288
2991.24809667950
3116.62975342654
3277.83474067274
3403.21639741979
3528.59805416684
3689.80304141303
3815.18469816008
3976.38968540628
4245.06466414995
4388.35798614657
4764.50295638771
5445.14623587167
6161.61284585479
6448.19948984803
6591.49281184467
6734.78613384129
6860.16779058832
7003.46111258495
7146.75443458158
7254.22442607904
7415.42941332525
7540.81107007228
7702.01605731850
7827.39771406555
7952.77937081258
8096.07269280920
8239.36601480582
8382.65933680246
8508.04099354949
8633.42265029654
8794.62763754275
8920.00929428978
9063.30261628642
9152.86094253430
9152.86094253430
9027.47928578725
8866.27429854105
8740.89264179401
8615.51098504696
8472.21766305034
8346.83600630329
8203.54268430667
8042.33769706047
7934.86770556300
7773.66271831680
7648.28106156975
7504.98773957313
7361.69441757651
7236.31276082946
7093.01943883284
6949.72611683622
6824.34446008917
6645.22780759340
6537.75781609593
6394.46449409930
6286.99450260183
6125.78951535563
5982.49619335901
5857.11453661196
5713.82121461534
5570.52789261872
5427.23457062210
5266.02958337590
5158.55959187843
5015.26626988181
4889.88461313476
4746.59129113814
4477.91631239447
4334.62299039784
4191.32966840122
3904.74302440798
3779.36136766093
3636.06804566431
3492.77472366768
3349.48140167106
3206.18807967444
3080.80642292739
2955.42476618035
2794.21977893415
2650.92645693753
2507.63313494090
2364.33981294428
2238.95815619723
2095.66483420061
1934.45984695440
1809.07819020737
1683.69653346032
1432.93321996623
841.848266730160
698.554944733535
555.261622736914
411.968300740292
286.586643993247
143.293321996624
17.9116652495797]; %Percent Volume of Particles
PDsoft = [
1.39561666594512e-07
1.49950026858291e-07
1.62772596681806e-07
1.73104098138666e-07
1.85989210350931e-07
2.03974950973525e-07
2.19157954495947e-07
2.35471114416798e-07
2.55606788076154e-07
2.80324767054815e-07
3.26946441391442e-07
3.58563188842820e-07
4.18196935782004e-07
4.44740729635290e-07
4.82771526818800e-07
5.24054424469023e-07
7.12863997280553e-07
7.81800184116777e-07
8.39993971602065e-07
9.11823777690583e-07
1.05262023625834e-06
1.14263220064314e-06
1.24034129401455e-06
1.33266687686749e-06
1.44662627012767e-06
1.55430680467442e-06
1.67000260740738e-06
1.81280835067790e-06
1.96782573973903e-06
2.13609902035778e-06
2.29510089188927e-06
2.46593816754275e-06
2.67680618134891e-06
2.90570600139896e-06
3.12199405169044e-06
3.38896290494868e-06
3.64122248622380e-06
3.99334013360134e-06
4.29058691329328e-06
4.56291902477348e-06
4.90256276678696e-06
5.32179212411611e-06
5.77687074282286e-06
6.27086417525666e-06
6.73763988672092e-06
7.23916034128975e-06
7.85819750243366e-06
8.53016994734126e-06
9.25960429322238e-06
9.74684685879053e-06
1.03654986747238e-05
1.09109336639234e-05
1.12518761785959e-05
1.17230958606127e-05
1.22140498505082e-05
1.25957118602532e-05
1.28567560393131e-05
1.33951868215624e-05
1.36727999954731e-05
1.41000447168378e-05
1.43922659615480e-05
1.48419924010910e-05
1.51495903967377e-05
1.54635633132419e-05
1.57840432698520e-05
1.61111651239670e-05
1.66146033558236e-05
1.69589384391874e-05
1.73104098138666e-05
1.76691653783942e-05
1.78513217865482e-05
1.84091360605235e-05
1.89843807952776e-05
1.95776006541100e-05
2.01893573198421e-05
2.12517260719164e-05
2.28336116429998e-05
2.45332458596066e-05
2.60904199202717e-05
2.69056877653959e-05
2.77464309252648e-05
2.86134454470491e-05
2.92064546758781e-05
2.98117539292038e-05
3.10602466235694e-05
3.20308105474619e-05
3.30317024446567e-05
3.40638699971579e-05
3.51282904999339e-05
3.62259717862563e-05
3.81321909803373e-05
4.13929618625663e-05
4.40202557345369e-05
4.58637892555499e-05
4.72969311041618e-05
4.87748554618420e-05
4.97857067931576e-05
5.02989616828277e-05
5.08175078618808e-05
5.18706928480774e-05
5.24054424469023e-05
5.40429964712156e-05
5.46001409606273e-05
5.68867517136398e-05
6.04974673389142e-05
6.43373622886160e-05
6.56707441655476e-05
6.63477623846779e-05
6.70317601755317e-05
6.84209830316993e-05
6.91263542388002e-05
6.98389973165723e-05
7.05589872327810e-05
7.20213113238556e-05
7.35139418554165e-05
7.42718178111196e-05
7.50375069237524e-05
7.65926476423511e-05
7.73822628441308e-05
7.81800184116777e-05
7.89859982663177e-05
7.98002871945455e-05
8.06229708569440e-05
8.22938694511819e-05
8.39993971602065e-05
8.75172255286644e-05
9.21224026126577e-05
9.79695952986761e-05
0.000101030928197529
0.000103124778549427
0.000106347199508683
0.000108551228760787
0.000113097268433398
0.000114263220064314
0.000115441191819362
0.000117833692654768
0.000119048473418537
0.000120275777699737
0.000121515734606842
0.000124034129401455
0.000124034129401455
0.000126604717538454
0.000126604717538454
0.000127909921270961
0.000130560834594658
0.000130560834594658
0.000131906823053425
0.000133266687686749
0.000136028619165429
0.000137430976557116
0.000138847791246584
0.000138847791246584
0.000140279212278193
0.000141725390232841
0.000143186477243811
0.000144662627012767
0.000144662627012767
0.000146153994825929
0.000149183013750700
0.000150720983505381
0.000152274808623932
0.000153844652563771
0.000155430680467442
0.000158651957266524
0.000160287545029918
0.000161939994528759
0.000163609479595435
0.000165296175854416
0.000167000260740737
0.000168721913518662
0.000168721913518662
0.000170461315300538
0.000173994099680477
0.000175787853916121
0.000177600100469975
0.000179431029984561
0.000181280835067790
0.000183149710313221
0.000185037852320535
0.000185037852320535
0.000188872733174427
0.000200860862366512
0.000202931593634451
0.000205023672654925
0.000209272756542350
0.000213609902035778
0.000220284738434044
0.000224850105548023]; % Particle Size (Diameter [m])
Nb = 25; %Number of Bins
alpha = 0.001; %Volume Fraction of Inlet
aa = cumtrapz(PDsoft,PerVolsoft); % Cumulative Integral
aa = aa.*alpha;
figrue
semilogx(PDsoft,aa), hold on
grid on
[Y,PDedges] = discretize(aa,Nb);
for j = 1:Nb
for i = 1:length(aa)
if Y(i) == j
I(i,1) = aa(i);
end
end
a(j,1) = I(length(I))-I(j);
end
semilogx(PDedges(1:end-1),a)
grid on
Y() is the bin number that each element of aa falls into after being discretized. This code does work, however, I don't recover the same curve.
The problem i see is that matrix I() changes size each iteration of j. What i need to be doing is, for each iteration of the j loop, call the first element of aa() and the last element of aa() that are in each bin. Then calculate the difference between the two. I want to call the values of aa() rather than use the PDedges that come out of the discretize() function because they are actual data points within the set.
For example if the number of bins Nb = 25, then the number of elements that belong in the First bin is 76. I want to call aa(76) and aa(1) without knowing exactly the element number becasue their location changes.
If I change the code to:
Nb = 25; %Number of Bins
alpha = 0.001; %Volume Fraction of Inlet
aa = cumtrapz(PDsoft,PerVolsoft); % Cumulative Integral
aa = aa.*alpha;
figrue
semilogx(PDsoft,aa), hold on
grid on
[Y,PDedges] = discretize(aa,Nb);
for j = 1:Nb
for i = 1:length(aa)
if Y(i) == j
I(i,j) = aa(i);
end
end
a(j,1) = I(length(I))-I(j);
end
semilogx(PDedges(1:end-1),a)
grid on
I() returns as the matrix with the columns of the bin values at their location in aa() with zeros before and after the bin values.
Thank you for your help!
Best,
Keegan
0 件のコメント
回答 (1 件)
Siraj
2023 年 9 月 7 日
Hi! It is my understanding that after grouping the data into different bins using “discretize” function, you want to find which elements of “aa” belong to which bin so that we can find the first and last element of each bin and take their difference to calculate the actual bin width.
Based on my understanding, it seems that the issue with the provided code is that the matrix "I" does not accurately indicate which elements of the variable "aa" are present in each bin. Upon analyzing the matrix "I," I noticed that starting from column 2, all columns have their initial values set to 0.
To address this problem, an alternative approach is to create a cell array with 25 cells (representing the number of bins). Each cell will contain an array that identifies the elements belonging to the corresponding bin.
To determine the bin width, we can retrieve the list of elements belonging to each bin from the cell array. By obtaining this list, we can extract the first and last element of the bin, calculate the difference between them, and store it.
Refer to the following code for better understanding.
%% creating a cell array where each cell represents a bin and the cell itself contains values of "aa" that belong to the corresponding bin
bin_cell = cell(Nb,1);
for j = 1:Nb
for i = 1 : length(aa)
if Y(i) == j % means aa(i) belongs to the jth bin.
current_element_in_bin = bin_cell{j};
new_elements_in_bin = [current_element_in_bin aa(i)]; %appending this element to the array
bin_cell{j} = new_elements_in_bin;
end
end
end
for j = 1: Nb
%get the jth bin
elements_in_bin = bin_cell{j};
if(numel(elements_in_bin) == 0)
continue;
end
% get the first element of aa() that belongs to the jth bin
first = elements_in_bin(1);
% get the last element of aa() that belongs to the jth bin
last = elements_in_bin(end);
a(j,1) = first - last;
end
To gain a deeper understanding of cell arrays and how they can be utilized in the context mentioned here refer to the link below.
Hope this helps.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!