# How to compute the average of several runs of a function?

2 views (last 30 days)
phdcomputer Eng on 10 Dec 2019
Commented: phdcomputer Eng on 11 Dec 2019
I wrote these codes,I loaded the dataset(colon attached) and I randomly selected a part of the data for test and a part for train the classifiers and computed the accuracies.
I want to define a for loop to run these lines 4 times and save the results of the runs in an array and then compute the sum of the array and calculate the average of the 4 runs.I don't know how to add the elements of an array and save it to divide by 4 for average.I'm trying to calculate the average of accuracies.
I want to compute the average for Arforest, ADT , Ask seprately.
the classificationa function is defined for apply the 3 classifiers,and allaccuracydata function is defined for computing the accuracy of each classifier.
I added a for loop and then used sum for add the elements of each array(Arforest,ADT,Ask) and divided the sum by 4 to obtain average. Are these codes right for computing the true average of 4 runs of the program?
I'll be very gratefull to have your opinions. Thanks
clc;
clear;
close all;
tic
data=colon;
[n,m]=size(data);
rows=(1:n);
test_count=floor((0.2)*n);
sum_ens=0;sum_result=0;
it=4;
for k=1:it
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
test=data(test_rows,:);
train=data(train_rows,:);
xtest=test(:,1:m-1);
ytest=test(:,m);
xtrain=train(:,1:m-1);
ytrain=train(:,m);
[rforest, DT , sk ] = classificationa(xtest,xtrain,ytrain);
end
RFf=sum(Arforest);
averf=RFf/it;
avedt=DTf/it;
avesk=SKf/it;

Jeremy Marcyoniak on 10 Dec 2019
Could you use
mean
?
phdcomputer Eng on 11 Dec 2019
@Jeremy Marcyoniak Thanks, by using mean after the for loop the result is better and more logical than the codes I added in the question for computing averages.
should I use counter k for the line:
[rforest(k), DT (k), sk(k) ] = classificationa(xtest,xtrain,ytrain);