How to save answers in an array using for loop
2 次查看(过去 30 天)
显示 更早的评论
Hi, I want to save all the answers of 100 iterations using for loop. The code for my task is given below. The problem is that only the value of the last iteration has saved and all I want to save all these values in an array. Please help me to solve this problem.
x=load('Inputnew.txt');
a=readfis('IFDCD');
for i=1:100
for j=1:4
y(j)=x(i,j);
end
predicted=evalfis(y,a);
if(predicted<=0.2355)
answer=11;
elseif(predicted<=0.7841)
answer=12;
elseif(predicted<=1.385)
answer=13;
elseif(predicted<=1.945)
answer=14;
elseif(predicted<=2.295)
answer=15;
elseif(predicted<=2.795)
answer=16;
elseif(predicted<=3.135)
answer=17;
elseif(predicted<=3.465)
answer=18;
elseif(predicted<=4.065)
answer=19;
elseif(predicted<=4.405)
answer=10;
elseif(predicted<=5.015)
answer=110;
elseif(predicted<=5.725)
answer=21;
elseif(predicted<=6.352)
answer=22;
elseif(predicted<=6.681)
answer=23;
elseif(predicted<=7.291)
answer=24;
elseif(predicted<=7.628)
answer=25;
elseif(predicted<=8.238)
answer=26;
elseif(predicted<=8.567)
answer=27;
elseif(predicted<=9.241)
answer=28;
elseif(predicted<=9.573)
answer=29;
else
answer=30;
end
end
0 个评论
回答(2 个)
Star Strider
2016-3-31
The way you have written your code, this line:
elseif(predicted<=9.573)
will always be true for all the other conditions, so the ‘answer’ will always be 29 or 30.
You need to set up your logic beginning with:
if(predicted <= 0.2355)
answer=11;
elseif((predicted > 0.2355) & (predicted <= 0.7841))
answer=12;
. . .
and so for the others.
0 个评论
Image Analyst
2016-3-31
You need to index the answer array, otherwise you're just overwriting the same answer scalar at each iteration of i. So do this everywhere you have answer:
answer(i) = .........
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!