finding numbers below a threshold and doing calculations on them

조회 수: 3 (최근 30일)
C.G.
C.G. 2021년 10월 13일
댓글: C.G. 2021년 10월 15일
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

채택된 답변

Voss
Voss 2021년 10월 13일
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
Voss
Voss 2021년 10월 14일
Try changing V1 to V1(a) in the line where V1 is used to calculate GT1(a).
C.G.
C.G. 2021년 10월 15일
Thank you for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by