MATLAB: How to compute the average of several repeats of a program in matlab

matrix arrayStatistics and Machine Learning Toolbox

I wrote this program to classify the dataset(colon) and compute the accuracy of the classifier, I wanted to repeat the classification 10 times and save the results in an array(Arforest)and then compute the average of these results(averf) but when I test size(Arforest) the size of the result is 1 so any array isn't shaped.
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=10;
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
averf=mean(Arforest);
avedt=mean(ADT);
avesk=mean(Ask);
Arforest is the result of Random Forest classifier,
when I use the counter (k) an error shows:
it=10;
for k=1:it
[rforest, DT , sk ] = classificationa(xtest,xtrain,ytrain);
[Arforest(k), ADT(k) , Ask(k)] = allaccuracydata(rforest(k), DT(k) , sk(k) , ytest);
end
averf=mean(Arforest);
avedt=mean(ADT);
avesk=mean(Ask);
the error is:
Error: File: myFSmethod20.m Line: 157 Column: 19
An array for multiple LHS assignment cannot contain expressions.
I'll be gratefull to have your opinions to obtain true result.Thanks

Best Answer

An array for multiple LHS assignment cannot contain expressions.
in
[Arforest(k), ADT(k) , Ask(k)] = allaccuracydata(rforest(k), DT(k) , sk(k) , ytest);
the arguments to allaccuracydata aren't arrays; they're the single-case results from the previous line. Just assign the results:
[Arforest(k) ADT(k) Ask(k)] = allaccuracydata(rforest,DT,sk,ytest);
each loop.
It would be better to preallocate the LHS arrays prior to beginning the loop.
Related Question