Mean of matrices calculation

조회 수: 1 (최근 30일)
Tahereh Goorangi
Tahereh Goorangi 2020년 4월 28일
댓글: darova 2020년 5월 11일
I have 200 .vec files which represent 200 time instances. In each file I have a matrix 'u', velocity, which is 79by49 matrix. I need to find the time average velocity field. Therefore I need to find the average of all the u matrices of all the 200 time instances.
How can I find the mean of all 200, 79by49 u matrices in form of a new 79by49 matrix ?
I would appreciate any suggestions.
Here is my code so far.
thank you,
Tara
clear all % clears all the variables which were open from prev. tasks
clc % clears all the previous commands
close all
files = dir('*.vec');
I = 79;
J = 49;
ScaleFactor = 5.09475; %px/mm
dt = 50e-6; %sec
Fs = 1000; %Hz
tf = 0.2; % 0.2s is the toal time taken to obtain the data for all datapoints ;
ti = 0.001; % is time between two data files
t = ti:ti:tf; % describes the full time taken to capture all the 200 files including time intervals
b1 = 153.6876; % length of the plain cylinder
b2 = 153.6876; % length of the stepped cylinder
u1 = zeros(200,1); % introducing a 200by1 matrix for all the u results at location 1(mid-span)
u2 = zeros(200,1); % introducing a 200by1 matrix for all the u results at location 2(b1/4 above mid-span)
u3 = zeros(200,1); % introducing a 200by1 matrix for all the u results at location 3(b1/4 below mid-span)
v1 = zeros(200,1); % introducing a 200by1 matrix for all the v results at location 1(mid-span)
v2 = zeros(200,1); % introducing a 200by1 matrix for all the v results at location 2(b1/4 above mid-span)
v3 = zeros(200,1); % introducing a 200by1 matrix for all the v results at location 3(b1/4 below mid-span)
for p = 1:length(files)
data_vec = dlmread(files(p).name, ',',1,0);
cntr = 1;
K = 1;
L = 1;
x_grid = (data_vec(1:I,1))/ScaleFactor; %mm
y_grid = (data_vec(1:I:end,2))/ScaleFactor; %mm
u = zeros(I,J);
v = zeros(I,J);
while L<=J
u(K,L) = data_vec(cntr,4)/(ScaleFactor*dt*1000); %m/s
v(K,L) = data_vec(cntr,3)/(ScaleFactor*dt*1000); %m/s
cntr = cntr+1;
K = K+1;
if K-1 == I
K = 1;
L = L+1;
end
end
u1(p) = u(16,24); % to save all the u(16,24) or u at location 1 in the u1 matrix
u2(p) = u(16,12); % to save all the u(16,12) or u at location 2 in the u2 matrix
u3(p) = u(16,36); % to save all the u(16,36) or u at location 3 in the u3 matrix
v1(p) = v(16,24); % to save all the v(16,24) or v at location 1 in the v1 matrix
v2(p) = v(16,12); % to save all the v(16,12) or v at location 2 in the v2 matrix
v3(p) = v(16,36); % to save all the v(16,36) or v at location 3 in the v3 matrix
[x,y] = meshgrid(x_grid,y_grid);
x = x';
y = y';
end
  댓글 수: 3
Tahereh Goorangi
Tahereh Goorangi 2020년 5월 11일
Hi Darova,
thank you for your reply.
your answer helped a lot.
darova
darova 2020년 5월 11일
you are welcome

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

채택된 답변

Sai Sri Pathuri
Sai Sri Pathuri 2020년 5월 5일
Hi,
You can find the sum of all velocity matrices and find the average as
% average = (velocityMatrix1 + velocityMatrix2 + ... + velocityMatrix200)/200;
To find the sum and mean of velocity matrices, you may use for loop as mentioned by darova
sum = 0;
for i = 1:200
% load the file corresponding to ith time instant. You may uncomment below code if your files are named u1.vec, u2.vec, ... and so on
% filename = append("u",string(i),".vec");
% load(filename);
sum = sum + u;
end
average = sum/200;
  댓글 수: 1
Tahereh Goorangi
Tahereh Goorangi 2020년 5월 11일
Hello there,
Thank you so much for the reply.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Monte Carlo Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by