• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Matlab combinatorics/matrix question

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

attack

Member
Joined
May 23, 2002
Hey all, I'm trying to do a complex simulation in Matlab and need some help!

I've created a program that takes a teams baseball batting average and creates a matrix (essential a CDF) for each battery with each row representing the batter and each column reprsents strikeout%, walk% etc...and runs it through a gaming simulation.

What I want to do, however, is change the batting order and re-run it for every different batting order to find the optimum order. Now I know there's 9! different ways to arrange the batters, but what I need is an easy way to code this and let the computer take care of the 360 million ways it's done.

So right now I have a 9x6 matrix that I want to rearange 9! ways with each row being the same.

I was thinking of two nested for loops but I can't think how to stop it when an order is set, run the simulation and repeat.

I don't think it's too hard to code, only a few lines, but just can't figure it out!
 
Well right now this is how I'm doing it...Order=[1 ;2 ;3 ;4 ;5 ;6 ;7 ;8 ;9];
Z=Order;
n=0;
for a=1:9
for b=1:9
for c=1:9
for d=1:9
for e=1:9
for f=1:9
for g=1:9
for h=1:9
for i=1:9
Order=[a ;b ;c ;d ;e ;f; g; h; i];
A=sortrows(Order);
if A==[1; 2; 3; 4; 5; 6; 7; 8; 9]
n=n+1
Z:),n)=Order;
end
end
end
end
end
end
end
end
end
end
So I now have a 9 by 9! matrix which I can call in a for loop!....bad news is this thing takes over 2 hours just to get the matrix, no idea how long the final simulation will take :/
 
I can't currently think of a better way of organising your nested loops, but instead of creating the huge Matrix and having to store all the data at once, I'd run the simulation inside the loops and store the current most optimal matrix for comparison with future orders.
 
It took over 3 hours to create, but I got a nice unique 9x9! matrix, had to transpose it to export to excel and ended up with a 12MB file. How I just call up batting order in a massive 1:362880 For Loop and run em through 10000 games, that's set to take 10 hours, we'll see what happens!

I'm not too familiar with memory resouces/memory vs cpu speed and whats critical here, I know my CPU cores are never more than 75% on any one and only 2 or 3 were ever really active during the loop. Does this mean I'm memory constrained (memory speed)?
 
I think you are going about it the long way in terms of creating your matrix... I've done 16x16 equations taking much quicker to calculate.

But then the amount of time you are experiencing may be from the inherent clumsiness of matlab.
 
Matlab has limited capability using multiple threads, I have never been able to get it to use more than 2 cores - I'd try rewriting it in C++ so you can just write the output to a text file line by line. C++ will be much faster than Matlab (even without making it parallel), I avoid Matlab for running simulations but use it afterwards as an analysis tool.

Matlab will be storing that matrix in your RAM/page file all at once, which takes quite a bit of memory due to its size.
 
Back