How do i create a program that, depending on the input value would create more or less variables
Show older comments
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
on 28 Mar 2018
Edited: Bob Thompson
on 28 Mar 2018
This is likely more than possible to do, but I'm a bit confused as to what exactly you're trying to create, and what your creation condition is. Would you be willing to restate it in a different manner for me?
In particular, I'm confused how k and p relate.
Feliciano Döring
on 28 Mar 2018
Stephen23
on 28 Mar 2018
"but give a number and the code creates the variables depending on how high the number is..."
Don't do this. Dynamically creating and accessing variable names is how beginners force themselves into writing slow, complex, buggy code that is hard to debug. The MATLAB documentation specifically advises against what you are trying to do: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
As the MATLAB help clearly states, you would be much better off putting the data into one array. In your case you could probably easily vectorize this code anyway, so loops are not required at all. Or use indexing: indexing is neat, simple, easy to debug, and very efficient. Read more here:
Feliciano Döring
on 28 Mar 2018
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
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.
Accepted Answer
More Answers (0)
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!