# MATLAB: Automatic creation of variables and expansion of matrices in m-files

• Jun 1st 2010, 12:47 AM
Hybz
MATLAB: Automatic creation of variables and expansion of matrices in m-files
Hello everyone. I am new to these forums, so apologies if I've inadvertantly posted this in the wrong section of the forum..

I needed some help with a MATLAB m-file I am constructing.

Currently, I have a series of while loops, within while loops generating a large number of calculations. The number of calculations done is about 2.4mil, but the number of answers generated are only a few thousand (as many of the calculations result in a common answer).

Everytime a NEW answer is generated, I am using the eval function to generate a variable with the name of the answer.

What I need to do, but am not sure how to, is somehow link all the scenarios to their respective answer.

For example, if Answer No. (and hence variable number) is 5,000, then I need to somehow create a matrix from this variable which lists all the loops out of the 2.4million that resulted in this answer.

Hopefully I've explained this clearly enough.

I thank you in advance for taking the time to read my dilemma.
• Jun 1st 2010, 01:05 AM
elbarto
If I understand your problem correctly, then there are a number of ways to do this. Since you have a relatively small data set (2.4 million elements will take up ~ 20MB so storing that in RAM is no problem), one option is to use "unique" and "find" functions as the most simple method.

Another option is to store duplicate data points as they occur instead of waiting until the end which sounds like what you are trying to do at the moment which I expect would be a more efficient method.

If you post up what you have so far I might be able to hack a few lines out to show how you could approach the problem.

Regards Elbarto
• Jun 1st 2010, 02:05 AM
Hybz
I am very new to MATLAB (and programming), so I have probably taken the "long way" with a lot of my programming methods.

The program as it currently stands, generates 7-digit quinary numbers as answers.
The variables making up all the loops are:
Region (48 conditions)
Xfield (9 conditions between 50 and 300)
Yfield (9 conditions between 50 and 300)
Zfield (5 conditions between 10 and 30)
Scenario (124 conditions)

(48 x 9 x 9 x5 x 124 = 2,410,560 loops)

Here's the crux of my program so far:

clear all;
Zfield = 10;
Yfield = 50;
Xfield = 50;

while (Xfield < 300.5)
while (Yfield < 300.5)
while (Zfield < 30.5)
sena = [0 Yfield 0]; senb = [0 (0.5*Yfield) 0]; senc = [0 0 0]; send = [(0.3333*Xfield) 0 0]; sene = [(0.6667*Xfield) 0 0]; senf = [Xfield 0 0]; seng = [0 0 Zfield];

region = 1;
x_upper = 0;
x_lower = 0;
y_upper = 0;
y_lower = 0;
z_upper = 0;
z_lower = 0;
interval = 0.5;
scenario = 1;
vel = 299792458;
x_offset = 0;
y_offset = 0;
z_offset = 0;

while (region < 48.5)

***Bunch of
conditionalcalcstodeterminexyzlower, and xyzupperbasedonregion***

Xfault = ((x_upper + x_lower)/2)+x_lower;
Yfault = ((y_upper + y_lower)/2)+y_lower;
Zfault = ((z_upper + z_lower)/2)+z_lower;

while (scenario < 124.5)

if (scenario > 0.5 && scenario < 24.5)
x_offset =
'zero';
elseif (scenario > 24.5 && scenario < 49.5)
x_offset =
'negative';
elseif (scenario > 49.5 && scenario < 74.5)
x_offset =
'positive';
elseif (scenario > 74.5 && scenario < 99.5)
x_offset =
'Xneg';
else x_offset = 'Xpos';
end

***If-loops to assignstringsof'zero', 'negative', 'positive', 'Xneg', or 'Xpos' to variablesx, y, z offset***

if (strcmp(x_offset,'positive'))
Xiter1 = Xfault + (Xfield/24);
elseif (strcmp(x_offset,'negative'))
Xiter1 = Xfault - (Xfield/24);
elseif (strcmp(x_offset,'Xpos'))
Xiter1 = Xfault - (Xfield*1.5/24);
elseif (strcmp(x_offset,'Xneg'))
Xiter1 = Xfault - (Xfield*1.5/24);
else Xiter1 = Xfault;
end
***Above if-loop repeaqted for'y'and'z'calcs***

iter1 = [Xiter1 Yiter1 Ziter1];
fault = [Xfault Yfault Zfault];

toa_a = ((sqrt(((Xfault - sena(1,1))^2)+((Yfault - sena(1,2))^2)+((Zfault- sena(1,3))^2))/vel)*10^9);
***Above line
repeatedforvariablestoa_bthroughtotoa_g***

toas = [toa_a toa_b toa_c toa_d toa_e toa_f toa_g];
toa_min = min(toas);

tdoa_a = toa_a - toa_min;
***Above line
repeatedforvariablestdoa_bthroughtotdoa_gusingtoa_ bthroughtotoa_grespectively***

toa_a_iter1 = ((sqrt(((Xiter1 - sena(1,1))^2)+((Yiter1 - sena(1,2))^2)+((Ziter1- sena(1,3))^2))/vel)*10^9);
***above line
repeatedforvariablestoa_b_iter1throughtotoa_g_iter 1usingsenbtosengrespectively***

toas_iter1 = [toa_a_iter1 toa_b_iter1 toa_c_iter1 toa_d_iter1 toa_e_iter1 toa_f_iter1 toa_g_iter1];
toa_min_iter1 = min(toas_iter1);

tdoa_a_iter1 = toa_a_iter1 - toa_min_iter1;
***above line
repeatedforvariablestoa_b_iter1throughtotoa_g_iter 1***

tdoas_iter1 = [tdoa_a_iter1 tdoa_b_iter1 tdoa_c_iter1 tdoa_d_iter1 tdoa_e_iter1 tdoa_f_iter1 tdoa_g_iter1];

sena_error_iter1 = tdoa_a_iter1 - tdoa_a;
***above line
repeatedforvariablessenb_error_iter1throughtoseng_ error_iter1***

if (sena_error_iter1 > 0 && sena_error_iter1 < 2)
a_error =
'pos'; a_num = 2*10^6;
elseif (sena_error_iter1 < 0 && sena_error_iter1 > -2)
a_error =
'neg'; a_num = 10^6;
elseif (sena_error_iter1 >= 2)
a_error =
'Xpos'; a_num = 4*10^6;
elseif (sena_error_iter1 <= -2)
a_error =
'Xneg'; a_num = 3*10^6;
else a_error = 'zero'; a_num = 0; ref = 'a';
end

***Above if-loop repeated forcalcs'b'to'g', using exponents^5to^0respectively***

num = a_num + b_num + c_num + d_num + e_num + f_num + g_num;
sum_offset = [
'x: ',x_offset,' y: ',y_offset,' z: ',z_offset];
instance = [
'Region:', {region},'Scenario:', {scenario},'Effect Code:',{num};];
eval([
'correction_' num2str(num) '=looped']);

scenario = scenario+1;
looped = looped + 1;
end
scenario = 1;
region = region + 1;
end
region = 1;
Zfield = Zfield + 5;
end
Zfield = 10;
if (Yfield < 199)
Yfield = Yfield +25;
else Yfield = Yfield +50;
end
end
Yfield = 50;
if (Xfield < 199)
Xfield = Xfield +25;
else Xfield = Xfield +50;
end
end

Thankyou! :)
• Jun 1st 2010, 02:29 AM
Hybz
Sorry, forgot to add that I'm after a series of matrices which will have all the information in this kind of form:

VariableName =['Region', {'X'}, {'Y'}, {'Z'}, {'Scenario'};
{Region}, {X}, {Y}, {Z}, {Scenario};
{Region}, {X}, {Y}, {Z}, {Scenario}];

so it'll be easy to just look at an answer, and automatically see a list of conditions which lead to that answer.

Thanks again :)
• Jun 1st 2010, 11:59 PM
elbarto
Hybz,

I am going to be honest with you and say I have got no idea what is going on here. Would you mind writing a bit of sudo code to help me better understand what you are trying to do.

ie. for example:

1)generate number x
2)compare value x with y
3) if x ?<>~=? y do this ....

My apologies I couldn't be more helpful. I

Elbarto
• Jun 2nd 2010, 02:15 AM
Hybz
lol, no worries.

a = 0;
b = 0;

while (b < 5)
while (a < 10)

x = a*b;

if (x is unique)
eval('variable_' num2str '=x')
else
end

a=a+1;
end
a=0;
b=b+1;

I'm not very good at pseudo-code, but basically imagine that this loop runs 50 times.

'a' and 'b' start at zero, and it calculates the factor of a*b.
The first loop will be 0x0, the second will be 0x1, until it reaches 5x10

Everytime a NEW answer for 'x' appears, it creates a variable with that name.

In this example, the result will be a list of variables of every number from 0 - 50, excluding the prime numbers.

Some loops will create a common answer for 'x'.

For example
a*b will equal 12 on 3 occasions:

a=3, b=4
a=4, b=3
a=6, b=2

I would like this to result in a matrix like:
Variable_12 =[{'a'}, {'b'};
{3}, {4};
{4}, {3};
{6}, {2}];

My actual program has 5 conditions instead of the 2 ('a' and 'b') shown here, and the calcs are a bit more complex, but that is basically the jist of it.
Hope that's a little more clear. :)
• Jun 7th 2010, 02:28 AM
elbarto
Try the following:

Code:

```an = 9; bn = 4; %build map with "double" key and "array" value myMap = containers.Map(0,[0 0]); myMap.remove(0); for a = 1:an     for b = 1:bn         x = a*b;         if any(cell2mat(myMap.keys())== x) == 0;             myMap(x) = [a,b];         else             myMap(x) = [myMap(x);[a b]];         end     end end for i = cell2mat(myMap.keys())     eval(sprintf('variable_%i = myMap(i)',i)) end```
Im not sure if this is exactly what you are looking for, but I would be inclined to make use of the containers.Map class to keep the code as tidy as possible. I dont use it much, but it seems like the right application for it here.

Regards Elbarto
• Jun 9th 2010, 07:56 PM
Hybz
Perfect. Thankyou very much elbarto. :)
• Jun 10th 2010, 03:36 AM
elbarto
No problem mate - good luck with it all.