creating multiple arrays that all have the same name with different number

5 views (last 30 days)
I have this code where i am calculating different arrays all use the same equation so i want to make a loop that can calculate them all in one go instead of having to write the same equation for each of them this is the code:
density = 1000;
g = 9.8;
gamma = density*g;
zr = 0;
z0 = 0.1;
z1 = 2.45;
z2 = 1.11;
z3 = 1.04;
Gcorrect2 = [0.43 0.916 1.402 1.92 2.43]';
G0cr = Gcorrect2 - gamma*z0*10^-5;
G1cr = Gcorrect2 - gamma*z1*10^-5;
G2cr = Gcorrect2 - gamma*z2*10^-5;
G3cr = Gcorrect2 - gamma*z3*10^-5;

Accepted Answer

Matt J
Matt J on 21 May 2023
Edited: Matt J on 21 May 2023
You wouldn't do that. You would instead combine all the calculations into a single matrix Gcr:
density = 1000;
g = 9.8;
gamma = density*g;
z=[0.1, 2.45, 1.11 1.04];
Gcorrect2 = [0.43 0.916 1.402 1.92 2.43]';
Gcr=Gcorrect2 - gamma*z*10^-5
Gcr = 5×4
0.4202 0.1899 0.3212 0.3281 0.9062 0.6759 0.8072 0.8141 1.3922 1.1619 1.2932 1.3001 1.9102 1.6799 1.8112 1.8181 2.4202 2.1899 2.3212 2.3281

More Answers (0)

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!