finding numbers below a threshold and doing calculations on them

I have a model of a box filled with particles. I have the velocity and coordinates of each particle in the box. I have divided the box into 4 segments based on coordinates and want to do calculations on the particles in each segment.
I have written a code which tells me how many particles are in each segment in each time step. But I now it to identify which particles are in each segment of the model, and calculate the average velocity per segment based off individual particel velocities.
file = dir('*.csv');
num_files = length(file);
[~, index] = natsort({file.name});
filelist = file(index);
rows = zeros(1, num_files);
for a = 1:num_files
T = table2array(readtable(filelist(a).name)); %read the data into matlab
%sum how many particles are in each of the 4 segments of the model
S1(a) = sum(T(:,4) >0.13); %first segment thresholds
S2(a) = sum(T(:,4)>= 0.115 & T(:,4) <= 0.13); %second segment thresholds
S3(a) = sum(T(:,4)>= 0.1 & T(:,4) <= 0.115); %third segment thresholds
rows(a) = height(T); %record how many rows are in the whole table (e.g. how many total grains in pile)
ResV(a) = sqrt((T(:,2).^2) + (T(:,3).^2) + (T(:,4).^2)); %calculate the resultant velocity of each particle in each time step
end

 Accepted Answer

idx = T(:,4) > 0.13; % similarly for other thresholds
ResV(a) = mean(sqrt(sum(T(idx,2:4).^2,2)));
In your original code, note that a value of 0.115 in T(:,4) would be counted in S2 and S3 both and note that T(:,1) is not used. I don't know whether these are intentional.

3 Comments

I hadn't realised that 0.115 would be used in both. Thank you for identifying that.
I've used the wrong columns in the ResV calculation so should be T1, T2, T3, but I corrected that since I posted this.
I now want to calculate the average granular temperature (velocity of each particle - mean velocity of whole segment) for each segiment. I've tried to adapt your code to do this but I cant get it to work
for a = 1:num_files
T = table2array(readtable(filelist(a).name)); %read in the values
S1 = T(:,4) >0.13; %identify all the rows in column 4 over 0.13
p1(a) = sum(S1); %number of particles in seg1
gd1 = diff(p1);
e1 = abs(gd1-1); %efflux out of seg1
V1(a) = mean(sqrt(sum(T(S1,1:3).^2,2))); %mean resultant velocity in seg1
GT1(a)= mean(sqrt(sum(T(S1,1:3).^2,2))-V1); %mean GT in seg1
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Try changing V1 to V1(a) in the line where V1 is used to calculate GT1(a).

Sign in to comment.

More Answers (0)

Products

Asked:

on 13 Oct 2021

Commented:

on 15 Oct 2021

Community Treasure Hunt

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

Start Hunting!