finding numbers below a threshold and doing calculations on them

2 Ansichten (letzte 30 Tage)
C.G.
C.G. am 13 Okt. 2021
Kommentiert: C.G. am 15 Okt. 2021
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

Akzeptierte Antwort

Voss
Voss am 13 Okt. 2021
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 Kommentare
Voss
Voss am 14 Okt. 2021
Try changing V1 to V1(a) in the line where V1 is used to calculate GT1(a).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by