Question

I am trying to generate several combination of values that will be used as parameters in an equation. The problem ism, it is very computationally expensive and the machine gets hanged. When there are only 3-4 parameters, then for loop works alright and I get results within reasonable time. Otherwise, it just freezes!
Following, is the code. Can somebody please provide an efficient way to generate combinations of these numbers so that the whole process becomes faster and I don't have to wait for days to get the result.

UPDATE based on the reply in ANSWER1 : (Just a simple try)

>> a1 = 0.1;
>> b1 = 0.2;
>> a = a1 + [0 : 0.01 : 0.04]

a =

    0.1000    0.1100    0.1200    0.1300    0.1400

>> b = b1 + [0 : 0.01 : 0.04]

b =

    0.2000    0.2100    0.2200    0.2300    0.2400

>> aa, bb = ndgird(a, b)
Undefined function or variable 'aa'.

Few more Questions : (1) What is wrong in the way I did

(2) What does the term 0.04 mean?

(3) How do I specify the number of elements in each vector of parameters? In this example, only 5 values are generated. thank you

Was it helpful?

Solution

You might want to try ndgrid to generate an n-dimensional grid for the parameter values.

E.g.

xx, yy = ndgrid([1,2,3],[0.1, 0.2,0.3])

for 2d, add more vectors for n-d.

2D parameters array

If you then need to have the parameters stored in a 2d array, you could do e.g.

[xx(:), yy(:),...]

Specific values

In your case you need to use the vectors

a = a1 + [0 : 0.01 : 0.04]
b = b2 + [0 : 0.01 : 0.04]
c = c3 - [0 : 0.01 : 0.04] 
...

or alternatively

a = a1 + linspace(0, 0.04, 5)
b = b2 + linspace(0, 0.04, 5)
c = c3 - linspace(0, 0.04, 5) 
...

and so on, such that each vector corresponds to the values you need to scan over. Then use

[aa, bb, cc, ...] = ndgrid(a, b, c,...)

OTHER TIPS

Some things to consider:

1) unless you pre-allocate the array, the growth of stor_param will be slow as matlab keeps having to find more space, copying data in the process. Use

stor_param=zeros(12,bigN);

Note that bigN= 5^12; which is about 244 Million...

2) You are looping all parameters over all possible values of t -this is another very large multiplier.

These two things together tell me that even efficient code will take quite some time; I recommend that you think about what you really need to do (when you have these 4E10 results what will you do with them?...)

The fastest solution is the one that does the least work. Right now your "algorithm" is brute force...

edit I am pretty sure that you are trying to find an optimum of some sort. If that is indeed the case, there are very efficient methods built in to Matlab to make that possible. Most of these are in the optimization toolbox; there are many examples online on how to use these functions. They include various methods for adding constraints - either boundary constraints (parameters no larger than xx or smaller than yy), or linked constraints (aa < 2*bb). Using these types of techniques will allow you to find a good combination of parameters much more easily than your current approach. Because even with ndgrid, you are evaluating your function many, many times...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top