Structures with for loop
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a problem like
"Conversion to double from struct is not possible.
Error in calisma7 (line 9)
A(1,j).sample(1,i)=initialization(new_a,new_b,10);"
My code is below. Code creates structures in structure until condition satisfies. But,at second iteration,it gives above error. When i tried to make A(1,2).sample(1,1) = initial... , it gave correct answer, but it didn't in loop.
while sz>0.0001
for j=1:length(A)
for i=1:length(A(1,j).AntObserved)
p = A(1,j).AntObserved(i);
d = A(1,j).interval{p};
new_a =min(d);
new_b =max(d);
A(1,j).sample(1,i)=initialization(new_a,new_b,10);
B(1,j).sample(1,i)=UpdatePhe(10);
[A(1,j).sample(1,i),B(1,j).sample(1,i)]=initial2(A(1,j).sample(1,i),B(1,j).sample(1,i),10,f);
A(1,j).sample(1,i)=Moving(A(1,j).sample(1,i),B(1,j).sample(1,i),10);
temp1 = A.sample;
temp2 = B.sample;
end
end
A=temp1;
B=temp2;
sz=A.sz;
end
I couldn't find what problem is. About structure field name? In addition, i should add initialization function:
function [ StartingValue] = initialization( a,b,n )
% [a,b] is interval of function that minimum points will search.
% n is pre-assigned number, # of subinterval.
% Intervals and midpoint of these intervals are described.
% a=a;
% b=b;
% n=n;
sz=(b-a)/n;
StartingValue.sz = sz;
StartingValue.interval = {};
StartingValue.x ={};
StartingValue.fn =[];
StartingValue.eta =[];
StartingValue.allowed = [];
StartingValue.Ants = ones(n,1);
StartingValue.AntObserved = {};
for i=1:n
StartingValue.interval{i} = [a+(i-1)*sz a+i*sz];
StartingValue.x{i} = a+(i-0.5)*sz;
StartingValue.fn(i) =0;
end
end
7 Kommentare
Geoff Hayes
am 31 Jul. 2016
Ozge - you need to provide a working sample of your code that can easily demonstrate the problem. What is your UpdatePhe or initial2 methods? How do they relate to the code that you posted in your question? I also suggest using the MATLAB debugger to step through your code.
Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!