2 views (last 30 days)

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

load colon.mat

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);

[Arforest, ADT , Ask] = allaccuracydata(rforest, DT , sk , ytest);

end

RFf=sum(Arforest);

averf=RFf/it;

DTf=sum(ADT);

avedt=DTf/it;

SKf=sum(Ask);

avesk=SKf/it;

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495991-how-to-compute-the-average-of-several-runs-of-a-function#comment_776714

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495991-how-to-compute-the-average-of-several-runs-of-a-function#comment_776714

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495991-how-to-compute-the-average-of-several-runs-of-a-function#comment_776885

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495991-how-to-compute-the-average-of-several-runs-of-a-function#comment_776885

Sign in to comment.