Mean of matrices calculation

1 view (last 30 days)
Tahereh Goorangi
Tahereh Goorangi on 28 Apr 2020
Commented: darova on 11 May 2020
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 Comments
Tahereh Goorangi
Tahereh Goorangi on 11 May 2020
Hi Darova,
thank you for your reply.
your answer helped a lot.
darova
darova on 11 May 2020
you are welcome

Sign in to comment.

Accepted Answer

Sai Sri Pathuri
Sai Sri Pathuri on 5 May 2020
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 Comment
Tahereh Goorangi
Tahereh Goorangi on 11 May 2020
Hello there,
Thank you so much for the reply.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!