How to save the several results of a program in an array?

clc;
clear;
close all;
tic
load colon.mat
data=colon;
[n,m]=size(data);
%%
%supervised
d=10;
l=1;
t=1;
for i=1:n
if data(i,m)==0
data(i,m)=2;
end
end
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
%computing Distance measures
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a6(i)=fTonimotoDist(thisCol1,thisCol2);
end
%sorting the distances
[A6,indA6]=sort(a6,'descend'); %Tonimoto
%selecting Threshold
datas6=data(:,indA6(1:d));
data6=[datas6 data(:,m)];
%%data6 classify%%tanimoto
[n,m]=size(data6);
for k=1:it
test=data6(test_rows,:);
train=data6(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);
[Arforest6(k), ADT6(k) , Ask6(k)] = allaccuracydata(rforest, DT , sk , ytest);
end
averf6=mean(Arforest6);
avedt6=mean(ADT6);
avesk6=mean(Ask6);
x6=[averf6, avedt6 , avesk6];
disp('tanimoto'); disp(x6);
In this code d is the number of selected features(columns) so we use 10 features of the data(colon attached) to classify It, my question is suppose that we want to obtain the average of Arforest (averf6) once for d=10, once for d=20,for d=30,d=40 and d=50 and save the results of averf6 for each of them in one array(forexample a) to plot the array.
how can I save the results of these different runing the program, in one array based on changing in d [10 20 30 40 50], I have problem in this part, and also how to define d that have several values, should I define d as an array forexample d=[10,20,30,40,50]; ?
Thanks

 Accepted Answer

Assuming that those means are scalar values, then a simple loop:
dV = [10,20,30,40,50];
nV = numel(dV);
averf6 = nan(1,nV);
avedt6 = nan(1,nV);
avesk6 = nan(1,nV);
for jj = 1:nV
d = dV(jj);
... your code
averf6(jj) = mean(...)
avedt6(jj) = mean(...)
avesk6(jj) = mean(...)
end

8 Comments

@Stephen Cobeldick I was thinking that maybe I can use a for loop and define the Arforest6 and averf6 as matrices to save the results but I don't know how to say that the new result (for each d) goes to next row of the Arforest6 matrix. If I use your codes , It's not necessory to use another line of these kind of codes.
%In the beginning of the program
Arforest=matrix(5,10);
averf6=matrix(5,1);
for d=10:10:50
for k=1:it
test=data6(test_rows,:);
train=data6(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);
[Arforest6(k), ADT6(k) , Ask6(k)] = allaccuracydata(rforest, DT , sk , ytest);
end
averf6=mean(Arforest6);
avedt6=mean(ADT6);
avesk6=mean(Ask6);
end % end of d
plot(averf6);
@Stephen Cobeldick Thank tou very much, averf6, avedt6,avesk6 are the averages of Arforest6,ADT6,Ask6 respectively, Do you think I should change them(Arforest6,ADT6,Ask6) based on jj or dV?
"I didn't understand the line averf6 = nan(1,nV); well, may you explain It"
That line preallocates an output vector of NaN values, which are replaced by your scalar output data inside the loop (where the index determines which elements to replace):
"I was thinking that maybe I can use a for loop and define the Arforest6 and averf6 as matrices to save the results..."
Which is exactly why my code does.
"...but I don't know how to say that the new result (for each d) goes to next row of the Arforest6 matrix"
Use indexing, just like I did. You use indexing all the time in your code, so what is stopping you from using it to refer to the rows of an output matrix, just like my code does refers to the elements of a vector? Index to to get data out of an array, index to put data into an array. Indexing is what you need.
Because you did not write anything about the sizes of your output data I assumed that they are scalar. If they are non-scalar AND all have the same size then you can allocate them to an output array, e.g.
dV = [10,20,30,40,50];
nV = numel(dV);
nO = number of values in each output;
averf6 = nan(nV,nO); % prellocate output array
for jj = 1:nV
d = dV(jj);
... your code
averf6(jj,:) = [a vector with nO values] % assign to one row of AVERF6
end
@Stephen Cobeldick I edited the codes using the indexes for matrices but the result is NaN. how can I solve this.
clc;
clear;
close all;
tic
load colon.mat
data=colon;
[n,m]=size(data);
%%
%supervised
dV = [10,20,30,40,50];
nV = numel(dV);
Arforest66=nan(5,10);
averf6 = nan(5,1); % prellocate output array
l=1;
t=1;
for i=1:n
if data(i,m)==0
data(i,m)=2;
end
end
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
%computing Distance measures
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a6(i)=fTonimotoDist(thisCol1,thisCol2);
end
% %sorting the distances
[A6,indA6]=sort(a6,'descend'); %Tonimoto
%selecting Threshold
for jj = 1:nV
d = dV(jj);
datas6=data(:,indA6(1:d)); %Tonimoto
data6=[datas6 data(:,m)];
rows=(1:n);
test_count=floor((0.2)*n);
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
%%data6 classify%%tanimoto
[n,m]=size(data6);
it=10;
for k=1:it
test=data6(test_rows,:);
train=data6(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);
[Arforest6(k), ADT6(k) , Ask6(k)] = allaccuracydata(rforest, DT , sk , ytest);
end
Arforest66(jj,:)=Arforest6;
ADT66(jj,:)=ADT6;
Ask66(jj,:)=Ask6;
averf6(jj,1)=mean(Arforest66(jj,:));
avedt6(jj,1)=mean(ADT66(jj,:));
avesk6(jj,1)=mean(Ask66(jj,:));
end
x6=[averf6, avedt6 , avesk6];
disp('tanimoto'); disp(x6);
@Stephen Cobeldick In the program , I repeated the classification part ten times(k) so the size of Arforest6,ADT6,Ask6 is 1x10 when I used jj=1:5 I defined the size of Arforest66 5x10 , the result for each value of dV[10,20,30,40,50] is saved in one row.and finally computing the mean of every row in averf6 with size 5x1. Do you think this code is wrong?
@phdcomputer Eng: please indent your code consistently. Inconsistently indented code, such as yours, hides the code's structure and hides bugs. You should use the MATLAB editor's default indentation rules, which are applied by default. You can also apply the editor's indentation rules to existing code: select the entire code, the press ctrl+i.
"Do you think this code is wrong?"
The basic concept seems correct, but the inconsistent indentation makes it difficult to tell if it has been implemented correctly.
It is not clear to me why you only preallocated two ouput arrays, although there are apparently six of them. It is also unclear why you hardcoded the preallocated array sizes, even though you have the required array dimensions in the variables nV and it.
You can simplify your code by calling mean after the loops, e.g.:
dV = [10,20,30,40,50];
nV = numel(dV);
it = 10; % mmoved from inside your loops.
Arf6 = nan(nV,it); % preallocate.
ADT6 = nan(nV,it); % preallocate.
Ask6 = nan(nV,it); % preallocate.
for jj = 1:nV
d = dV(jj);
...
for k = 1:it
...
[Arf6(jj,k), ADT6(jj,k), Ask6(jj,k)] = allaccuracydata(...);
end
end
averf6 = mean(Arf6,2); % mean of each row
avedt6 = mean(ADT6,2); % mean of each row
avesk6 = mean(Ask6,6); % mean of each row
This assumes that the outputs of allaccuracydata are all scalars (which, judging by your code, they appear to be).
sorry, I don't know how to indent the codes well I use the code part , sure I use ctrl+i to indent the codes.I hope that the structue will be better. I used these codes based on your advices but still the results are NaN
clc;
clear;
close all;
tic
load colon.mat
data=colon;
[n,m]=size(data);
%%
%supervised
it=10;
dV = [10,20,30,40,50];
nV = numel(dV);
arf66=nan(nV,it);
adt66=nan(nV,it);
ask66=nan(nV,it);
l=1;
t=1;
for i=1:n
if data(i,m)==0
data(i,m)=2;
end
end
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
%computing Distance measures
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a6(i)=fTonimotoDist(thisCol1,thisCol2);
end
% %sorting the distances
[A6,indA6]=sort(a6,'descend'); %Tonimoto
%selecting Threshold
for jj = 1:nV
d = dV(jj);
datas6=data(:,indA6(1:d)); %Tonimoto
data6=[datas6 data(:,m)];
rows=(1:n);
test_count=floor((0.2)*n);
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
%%data6 classify%%tanimoto
[n,m]=size(data6);
for k=1:it
test=data6(test_rows,:);
train=data6(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);
[Arforest6(jj,k), ADT6(jj,k) , Ask6(jj,k)] = allaccuracydata(rforest, DT , sk , ytest);
end
averf6=mean(Arforest6,2);
avedt6=mean(ADT6,2);
avesk6=mean(Ask6,2);
end
x6=[averf6, avedt6 , avesk6];
disp('tanimoto'); disp(x6);
plot(averf6);
plot(avedt6);
plot(avesk6);
"...sure I use ctrl+i to indent the codes.I hope that the structue will be better."
Your code has exactly the same indentation as before. I cannot follow it.
Please align your code consistently in the MATLAB editor
  1. select all of the code
  2. press ctrl + i

Sign in to comment.

More Answers (1)

@Stephen Cobeldick I attaced the allaccuracydata,classificationa and ftanimotodist functions. Thanks greatly

8 Comments

@phdcomputer Eng: I cannot run your code without the badly named colon data.
Note that you would sgnificantly benefit from half an hour learning how to use the debugging tools, then you would learn how to debug your own code. Debugging your own code is much faster than relying on random strangers on the internet to do it for you.
Thanks greatly for your attention, yes as you adviced aligned the codes, thanks
clc;
clear;
close all;
tic
load CNS.mat
data=CNS;
[n,m]=size(data);
%%
%supervised
it=10;
dV = [10,20,30,40,50];
nV = numel(dV);
Arforest6=nan(nV,it);
Adt6=nan(nV,it);
Ask6=nan(nV,it);
averf6 = nan(nV,1);
avedt6 = nan(nV,1);
avesk6 = nan(nV,1);
l=1;
t=1;
for i=1:n
if data(i,m)==0
data(i,m)=2;
end
end
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
%computing Distance measures
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a6(i)=fTonimotoDist(thisCol1,thisCol2);
end
% %sorting the distances
[A6,indA6]=sort(a6,'descend'); %Tonimoto
%selecting Threshold
for jj = 1:nV
d = dV(jj);
datas6=data(:,indA6(1:d)); %Tonimoto
data6=[datas6 data(:,m)];
rows=(1:n);
test_count=floor((0.2)*n);
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
%%data6 classify%%tanimoto
[n,m]=size(data6);
for k=1:it
test=data6(test_rows,:);
train=data6(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);
[Arforest6(jj,k), ADT6(jj,k) , Ask6(jj,k)] = allaccuracydata(rforest, DT , sk , ytest);
end
averf6=mean(Arforest6,2);
avedt6=mean(ADT6,2);
avesk6=mean(Ask6,2);
end
x6=[averf6, avedt6 , avesk6];
disp('tanimoto'); disp(x6);
plot(averf6);
figure
plot(avedt6);
figure
plot(avesk6);
I understand but colon dataset is from UCI, my teachers are using colon and give the data to students.
Thanks, sure I try to learn debugging tools.
I run these codes but still the results are NaN. this time I tried CNS dataset (attached)instead of colon. but the result is:
tanimoto
0.5750 0.6667 0.6667
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
I do not have the randomforest function, which is called inside classificationa. Please provide this and any other functions needed, or links to where they can be download from.
@Stephen Cobeldick I attached randomforest and DTree functions that are used in classificationa function. I'll be very gratefull to have your opinion about how to obtain true result matrix without NaN values. Thank you very much
I do not have the Statistics Toolbox, so I cannot run this code.
You can debug your own code by investigating when and where those NaN values come from. For example, a simple way to start investigating this is to display the function outputs on each iteration (to check if they are non-NaN), e.g.:
...
for jj = 1:nV
d = dV(jj);
...
for k = 1:it
...
disp(k) % do you get 10 iterations?
[XX,YY,ZZ] = allaccuracydata(rforest, DT , sk , ytest) % display!
Arforest6(jj,k) = XX
ADT6(jj,k) = YY
Ask6(jj,k) = ZZ
end
end
% For some reason you moved these lines inside the loop, but they should be here:
averf6=mean(Arforest6,2);
avedt6=mean(ADT6,2);
avesk6=mean(Ask6,2);
Thanks , should I use these lines in the for k=1:it loop?
disp(k) % do you get 10 iterations?
[XX,YY,ZZ] = allaccuracydata(rforest, DT , sk , ytest) % display!
Arforest6(jj,k) = XX
ADT6(jj,k) = YY
Ask6(jj,k) = ZZ
and I put the averages lines out of the two loops but still shows the same result(Nan)
"should I use these lines in the for k=1:it loop?"
Yes. That is exactly what I showed you. Then you can check what results are generated on each loop iteration, and continue to investigate the iterations that are not generating the results that you expect.
@Stephen Cobeldick Thanks greatly, I Edited the codes :
clc;
clear;
close all;
tic
load colon.mat
data=colon;
[n,m]=size(data);
%%
%supervised
it=10;
dV = [10,20,30,40,50];
nV = numel(dV);
Arforest6=nan(nV,it);
Adt6=nan(nV,it);
Ask6=nan(nV,it);
averf6 = nan(nV,1);
avedt6 = nan(nV,1);
avesk6 = nan(nV,1);
l=1;
t=1;
for i=1:n
if data(i,m)==0
data(i,m)=2;
end
end
data1=[];
data2=[];
for i=1:n
if data(i,m)==1
data1(l,:)=data(i,1:m-1);
l=l+1;
else
data2(t,:)=data(i,1:m-1);
t=t+1;
end
end
if t>l
data1(l:t-1,1:m-1)=0;
else
data2(t:l-1,1:m-1)=0;
end
%computing Distance measures
for i=1: m-1
thisCol1=data1(:,i);
thisCol2=data2(:,i);
a6(i)=fTonimotoDist(thisCol1,thisCol2);
end
% %sorting the distances
[A6,indA6]=sort(a6,'descend'); %Tonimoto
%selecting Threshold
for jj = 1:nV
d = dV(jj);
datas6=data(:,indA6(1:d)); %Tonimoto
data6=[datas6 data(:,m)];
rows=(1:n);
test_count=floor((0.2)*n);
test_rows=randsample(rows,test_count);
train_rows=setdiff(rows,test_rows);
%%data6 classify%%tanimoto
[n,m]=size(data6);
for k=1:it
test=data6(test_rows,:);
train=data6(train_rows,:);
xtest=test(:,1:m-1);
ytest=test(:,m);
xtrain=train(:,1:m-1);
ytrain=train(:,m);
disp(k);
[rforest, DT , sk ] = classificationa(xtest,xtrain,ytrain);
[XX, YY , ZZ] = allaccuracydata(rforest, DT , sk , ytest);
Arforest6(jj,k) = XX;
ADT6(jj,k) = YY;
Ask6(jj,k) = ZZ;
end
end
averf6=mean(Arforest6,2);
avedt6=mean(ADT6,2);
avesk6=mean(Ask6,2);
x6=[averf6, avedt6 , avesk6];
disp('tanimoto'); disp(x6);
plot(averf6);
figure
plot(avedt6);
figure
plot(avesk6);
the results of disp(k) is countiong from 1 to 10 five times, In first iteration the XX,YY,ZZ are 0.8333,0.6667, 0.7500 for all the ten outputs, but for next four iterations all the outputs are NaN.
tanimoto
0.8167 0.5833 0.8333
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
and the results of plots are empty. It seems the codes are right but the results are NaN.

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!