How to display results for looping

4 views (last 30 days)
Suizwati
Suizwati on 18 Jul 2013
helle everyone.. I would like to ask a question regarding to my programming. I have this code as below. I want to display all the result for each iteration. which part do I need to put the syntax so that it can store the result from 1st iteration till 5th and display all the result (x1_new,x2_new,y_new) when the loop is done..
clc clear a=5; alpha=0.2
x1=ceil(rand(a,1)*10); x2=ceil(rand(a,1)*10); y=x1+x2 data=[x1 x2 y]
for n=1:5 for i=1:5 if y(n)<y(i)
r=(data(n,1)-data(i,1))^2+(data(n,2)-data(i,2))^2
p=sqrt(r)
x1_new=ceil(data(i,1)+exp(-p^2)*(data(i,1)-data(i,1))+alpha.*(rand-0.5))
x2_new=ceil(data(i,2)+exp(-p^2)*(data(i,2)-data(i,2))+alpha.*(rand-0.5))
y_new=x1_new(i)+x2_new
else
x1_new=data(i,1)
x2_new=data(i,2)
y_new=data(i,3)
end
end
end
end
tq for your help..

Answers (3)

per isakson
per isakson on 18 Jul 2013
The results need to be arrays, i.e.
x1_new(n,i)
x2_new(n,i)
y_new(n,i)

Suizwati
Suizwati on 18 Jul 2013
I have put that arrays as suggested.. But it doesn't woks and display an error (??? Subscripted assignment dimension mismatch.). How I can solve this problems? Is it correct the syntax that I have been put on my programming as below?
clc clear a=5; alpha=0.2
x1=ceil(rand(a,1)*10); x2=ceil(rand(a,1)*10); y=x1+x2 data=[x1 x2 y]
for n=1:5 for i=1:5 if y(n)<y(i)
r=(data(n,1)-data(i,1))^2+(data(n,2)-data(i,2))^2
distance=sqrt(r)
x1_new(n,i)=ceil(data(i,1)+exp(-distance^2)*(data(i,1)-data(i,1))+alpha.*(rand-0.5))
x2_new(n,i)=ceil(data(i,2)+exp(-distance^2)*(data(i,2)-data(i,2))+alpha.*(rand-0.5))
y_new(n,i)=x1_new(i)+x2_new
else
x1_new(n,i)=data(i,1)
x2_new(n,i)=data(i,2)
y_new(n,i)=data(i,3)
result(n,i)=[x1_new(n,i) x2_new(n,i) y_new(n,i)]
end
end
end

Image Analyst
Image Analyst on 18 Jul 2013
In the line
result(n,i)=[x1_new(n,i) x2_new(n,i) y_new(n,i)]
you're trying to stuff 3 elements into a single result element. You can't do that. You can put only 1 element into that element.

Community Treasure Hunt

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

Start Hunting!