Creating matrix from a loop between multiple files
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ben Bawtree
el 18 de Mzo. de 2022
Comentada: Ben Bawtree
el 18 de Mzo. de 2022
Hi! I am currently trying to format a code which runs through the same steps but for multiple different files which have been loaded into the data base. I am unsure as to how to compress it into a some sort of loop if that'd quicken it? This is the first set of code which I want to condense and obtain the outputs RS1, RS2 and RS3 in a matrix.
% Repeat process by calling and averaging the wanted columns in data file 'c1'.
uc1 = c1(:,3);
u1c1 = [mean(uc1) mean(uc1)];
wc1 = c1(:,5);
u3c1 = [mean(wc1) mean(wc1)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_1 = uc1-u1c1;
upr3_1 = wc1-u3c1;
% Compute the product of both deviations
uval1 = upr1_1.*upr3_1;
uva_1 = [mean(uval1) mean(uval1)];
RS1 = uva_1(1,2)/(10e-5);
% Repeat process by calling and averaging the wanted columns in data file 'c2'.
uc2 = c2(:,3);
u1c2 = [mean(uc2) mean(uc2)];
wc2 = c2(:,5);
u3c2 = [mean(wc2) mean(wc2)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_2 = uc2-u1c2;
upr3_2 = wc2-u3c2;
% Compute the product of both deviations
uval2 = upr1_2.*upr3_2;
uva_2 = [mean(uval2) mean(uval2)];
RS2 = uva_2(1,2)/(10e-5);
% Repeat process by calling and averaging the wanted columns in data file 'c3'.
uc3 = c3(:,3);
u1c3 = [mean(uc3) mean(uc3)];
wc3 = c3(:,5);
u3c3 = [mean(wc3) mean(wc3)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_3 = uc3-u1c3;
upr3_3 = wc3-u3c3;
% Compute the product of both deviations
uval3 = upr1_3.*upr3_3;
uva_3 = [mean(uval3) mean(uval3)];
RS3 = uva_3(1,2)/(10e-5);
Then this is another set, which I also want to loop and form a matrix of umc1:5 values.
%C1 @ 0.45m
uc1 = c1(:,3);
umc1 = mean(uc1);
%C2 @ 0.40m
uc2 = c2(:,3);
umc2 = mean(uc2);
%C3 @ 0.35m
uc3 = c3(:,3);
umc3 = mean(uc3);
%C4 @ 0.30m
uc4 = c4(:,3);
umc4 = mean(uc4);
%C5 @ 0.25m
uc5 = c5(:,3);
umc5 = mean(uc5);
Any help would be greatly appreciated, many thanks. Ben
4 comentarios
Jan
el 18 de Mzo. de 2022
Editada: Jan
el 18 de Mzo. de 2022
This comment does not increase the clarity. You want to modify the code to expand it from using one fileto using a bunch of files. Then the actual computations do not matter, but how you imprt the file. This means, that the posted code is not relecant to the question, but the important code is missing.
Respuesta aceptada
Stephen23
el 18 de Mzo. de 2022
Editada: Stephen23
el 18 de Mzo. de 2022
Use arrays and indexing rather than numbered variable names.
Use a more appropriate tool for importing textfile data, e.g. READMATRIX
P = 'absolute or relative path to where the files are saved';
V = 1:3;
RSV = nan(size(V));
for k = 1:numel(V)
F = sprintf('c%u.dat',V(k));
c1 = readmatrix(fullfile(P,F));
uc1 = c1(:,3);
u1c1 = [mean(uc1),mean(uc1)];
wc1 = c1(:,5);
u3c1 = [mean(wc1),mean(wc1)];
% Calculate standard deviation from the mean for u1 and u3.
upr1_1 = uc1-u1c1;
upr3_1 = wc1-u3c1;
% Compute the product of both deviations
uval1 = upr1_1.*upr3_1;
uva_1 = [mean(uval1),mean(uval1)];
RSV(k) = uva_1(1,2)/(10e-5);
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!