It is difficult to answer, because we don't know what you mean by database, or why you need recursivity (why a FOR loop is not enough, of why you even need such a loop).
If you are truly dealing with a database, you'll SELECT the relevant company and you can just use MEAN to compute the average (it will work with any data size). If you need/want to get all companies in one shot and then compute the average per company, you can proceed as follows. Note that I assume that companies have IDs (defined by the DBM, or by yourself) which are integers starting at 1.
I define a fake data set for the purpose of this example..
>> data = [randi(3,10,1), 20*rand(10,1)]
data =
3.0000 3.1523
3.0000 19.4119
1.0000 19.1433
3.0000 9.7075
2.0000 16.0056
1.0000 2.8377
1.0000 8.4352
2.0000 18.3147
3.0000 15.8441
3.0000 19.1898
Here, the first column is the company ID, and the second the stock price. Now we compute means using ACCUMARRAY and the company ID as vector of indices..
>> allMeans = accumarray( data(:,1), data(:,2), [], @mean )
allMeans =
10.1388
17.1602
13.4611
You see that we get a vector of three means, one for each company. ACCUMAARAY computes the sum by default, unless we pass the function that it has to use for accumulation as 4th argument. This is what we are doing, passing a handle on function MEAN (which is what we want to compute).
If it is not what you wanted, you'll have to make your question more precise about what you have and what you need.