loop to identify new particles

I have the particle Id and y coordinates for every particle in my model over 3000s.
My loop currently tells me how many grains in each second have y coordinates <-0.13 and sums this in the variable 'grains'. This loop includes all the grains <-0.13, even if they have already been included in prevoius time steps.
I want the loop to only include new grains to get mass efflux per second that is not cumulative, and I want it to use the particle ID to do this. For example if particle 2 was included in time step 2, do not include in time step 2.
Can anybody help?
ymax = -0.13;
for b = 1:length(particledata)
%save all the rows in the 1st and 6th column (ID and y-coordinates) of each cell as a new variable y
y{b} = particledata{b}(:,[1 6]);
%use the function table2array to turn the format of the data from a table to an array
y_array{b} = table2array(y{b});
%sum the total number of grains with y coordinate <-0.13 in each cell, and save into a new variable 'grains'
grains{b} = sum(y_array{b}(:,2)<ymax);
fprintf('A total of %d grains left the rice pile\n',grains{b});
end

Antworten (1)

Anmol Dhiman
Anmol Dhiman am 28 Jan. 2021
Bearbeitet: Anmol Dhiman am 28 Jan. 2021

0 Stimmen

Hi Chloe,
Assuming you have all the data in an array format. Follow the below instructions
% outside the loop
temp = [];
% Inside the loop after y_array{b} = table2array(y{b}); statement
temp2 = setdiff(y_Array{b}(:,2),temp);
grains{b} = sum(temp2<ymax);
temp = [temp temp2];
Hope it helps

5 Kommentare

C.G.
C.G. am 28 Jan. 2021
Thank you for your response.
I have added your code into my script, and it is coming up with the error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
The number of grains leaving the system could vary greatly in each time step. Does this pose a problem?
Hi Chloe,
It doesnot matter, the error is coming in
temp = [temp temp2];
would you mind sharing the dimensions of the temp and temp2 variables in your case
C.G.
C.G. am 29 Jan. 2021
temp is a 37x1 double and temp2 is a 39x1 double
Anmol Dhiman
Anmol Dhiman am 29 Jan. 2021
Use temp = [temp ; temp2]; It will work
Thanks
C.G.
C.G. am 29 Jan. 2021
Thank you for your response, the code does work but I am not entirely sure it is what I need it to do. Could you explain it a bit for me?
I need the code to find and save all the rows in the array where column 2 is <-0.13. Then I want it too look through the rows and if the number in the first column (particle ID) and only save the row where the first occurenence of this number occurs. I have recently adapted the code above to this.
for b = 1:length(particledata)
%get the particle ID (:,1) and coordinates (:,6) and save them in an array
y{b} = table2array(particledata{b}(:,[1 6]));
%find and save all the rows in the array where column 2 is <-0.13
grains{b} = y{b}(y{b}(:,2) <= ymax, :);
%if the number in the first column of each cell is in any of the previous columns,delete the row
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 26 Jan. 2021

Kommentiert:

am 29 Jan. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by