How do i create a program that, depending on the input value would create more or less variables

7 views (last 30 days)
I have the following code i did for calculating location of earthquakes based on the information of different stations, it isn't complete yet but it's something. How do i create an index or array structure in which i don't have to manually insert da,db,dc or dtax0, dtay0 and so on, but give a number and the code creates the variables depending on how high the number is, so for example if i put a for k=1:1 it just creates da,dtat0,dtax0,dtay0 and dtaz0 but if i put an 'if p=2' it creates da,db,dtax0,dtbx0 and so on.
da=((A(1,1)-m0(2))^2+(A(2,1)-m0(3))^2+(A(3,1)-m0(4))^2)^(1/2);
db=((A(1,2)-m0(2))^2+(A(2,2)-m0(3))^2+(A(3,2)-m0(4))^2)^(1/2);
dc=((A(1,3)-m0(2))^2+(A(2,3)-m0(3))^2+(A(3,3)-m0(4))^2)^(1/2);
dd=((A(1,4)-m0(2))^2+(A(2,4)-m0(3))^2+(A(3,4)-m0(4))^2)^(1/2);
dtat0=1;
dtax0=-(A(1,1)-m0(2))/v*da;
dtay0=-(A(2,1)-m0(3))/v*da;
dtaz0=-(A(3,1)-m0(4))/v*da;
dtbt0=1;
dtbx0=-(A(1,2)-m0(2))/v*db;
dtby0=-(A(2,2)-m0(3))/v*db;
dtbz0=-(A(3,2)-m0(4))/v*db;
dtct0=1;
dtcx0=-(A(1,3)-m0(2))/v*dc;
dtcy0=-(A(2,3)-m0(3))/v*dc;
dtcz0=-(A(3,3)-m0(4))/v*dc;
dtdt0=1;
dtdx0=-(A(1,4)-m0(2))/v*dd;
dtdy0=-(A(2,4)-m0(3))/v*dd;
dtdz0=-(A(3,4)-m0(4))/v*dd;
The da,db,dc,dd are the distances and the rest are the partial derivates
  6 Comments
Bob Thompson
Bob Thompson on 28 Mar 2018
It's fairly easy to have a user defined variable that can be a condition for the creation of other values. The simplest way is to hard code the value somewhere near the top of your code so it is relatively accessible. Alternatively you can use the input() function to prompt the user for an input.
I'm still not sure how you want to use this value, however, so I'm not entirely sure how to help you create more values with it. In the instance of a for loop, it is perfectly valid to use variables for the ranges (of k in your example) such as the following:
number = input('Enter the number of values to create: ');
for k = 1:number;
values(k) = k;
end
Similarly, you could use an if statement if you have a more broad range to consider:
number = input('Enter the number of values to create: ');
if number >= 1 && number < 6;
values = rand(3);
elseif number >= 6 && number < 100;
values = rand(10);
else
values = rand(1);
end
Stephen23
Stephen23 on 28 Mar 2018
Edited: Stephen23 on 28 Mar 2018
A useful tip for writing code: computers are only actually good at doing one thing: simple operations really quickly in loop. This means that anytime you find yourself copy-and-pasting code, you are just doing the computers job for it. Instead, put your data into vectors/matrices/arrays, do operations on the complete vector/matrices/arrays, and MATLAB will process your data quickly and efficiently. MATLAB has fast internal loops that do everything, exactly so that you do not have to write slow, complex code changing variable names...
Keep data together as much as possible, rather than splitting it apart, and you will make your code simpler and more efficient.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 28 Mar 2018
Edited: Stephen23 on 2 Apr 2018
Forget about dynamically accessing variable names, simply vectorize your code and you will immediately be on the way to writing simpler, more efficient MATLAB code. Rather than these:
da=((A(1,1)-m0(2))^2+(A(2,1)-m0(3))^2+(A(3,1)-m0(4))^2)^(1/2);
db=((A(1,2)-m0(2))^2+(A(2,2)-m0(3))^2+(A(3,2)-m0(4))^2)^(1/2);
dc=((A(1,3)-m0(2))^2+(A(2,3)-m0(3))^2+(A(3,3)-m0(4))^2)^(1/2);
dd=((A(1,4)-m0(2))^2+(A(2,4)-m0(3))^2+(A(3,4)-m0(4))^2)^(1/2);
Just write this:
dV = sqrt((A(1,:)-m0(2)).^2+(A(2,:)-m0(3)).^2+(A(3,:)-m0(4)).^2);
And rather than these:
dtax0=-(A(1,1)-m0(2))/v*da;
dtbx0=-(A(1,2)-m0(2))/v*db;
dtcx0=-(A(1,3)-m0(2))/v*dc;
...
do something like this (the exact code you need depends on the size of v):
dtx0 = -(A(1,:)-m0(2))./v.*d;
or even do all of x, y and z at once, something like:
dt0 = -bsxfun(@minus,A(1,:),m0(:))./v.*d;
The actual operations you require depend on the sizes and classes of the arrays.
Knowing how to manipulate matrices/arrays is fundamental to using MATLAB. The name MATLAB comes from "MATrix LABoratory" and not from "puts the data into lots of separate variable and make it difficult to work with". The introductory tutorials are a good place to start learning how to use matrices effectively:
You should also read these:
Experiment! The more you practice, the more it will make sense and the better you can use matrices.
  12 Comments
Feliciano Döring
Feliciano Döring on 4 Apr 2018
One last thing, how would i generalize the following:
dat=norm(A(:,5)-A(:,1));
dbt=norm(A(:,5)-A(:,2));
dct=norm(A(:,5)-A(:,3));
ddt=norm(A(:,5)-A(:,4));
i tried writing it as
dt=norm(A(:,5)-A(:,1:4));
but i want for the code to calculate for each element.

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!