how to collect variables in an aray in for loop?

function close_point(p0,p1,p2,p3)
for t=0:0.1:1
b0=(1-t).^2;
b1=2*t.*(1-t);
b2=t.^2;
x=b0*p0(1)+b1*p1(1)+b2*p2(1);
y=b0*p0(2)+b1*p1(2)+b2*p2(2);
xs=sqrt((p3(1)-x).^2+(p3(2)-y).^2)
end
i want to save xs values in a array.plz tell me how to save or put xs values in array?

 Accepted Answer

Hi
You have two ways to address your problem you can either use a separate variable or you use same variable with which you are running the loop
Method 1
function close_point(p0,p1,p2,p3)
Index=1;
for t=0:0.1:1
b0=(1-t).^2;
b1=2*t.*(1-t);
b2=t.^2;
x=b0*p0(1)+b1*p1(1)+b2*p2(1);
y=b0*p0(2)+b1*p1(2)+b2*p2(2);
xs(Index)=sqrt((p3(1)-x).^2+(p3(2)-y).^2)
Index=Index+1;
end
Method 2
function close_point(p0,p1,p2,p3)
for t=0:0.1:1
b0=(1-t).^2;
b1=2*t.*(1-t);
b2=t.^2;
x=b0*p0(1)+b1*p1(1)+b2*p2(1);
y=b0*p0(2)+b1*p1(2)+b2*p2(2);
xs(uint8((t*10)+1))=sqrt((p3(1)-x).^2+(p3(2)-y).^2)
end

3 Comments

This is poor advice as written. Because you don't preallocate the variable xs, that forces MATLAB to grow the vector at each step, which is very inefficient. As you should see, both KSSV and Stephen have indicated how to preallocate xs. That is very important in general, as otherwise your code will run slowly. On this particular problem, the code will not be very slow, because the variables are so small in size. But you need to learn about preallocation, as well as not teach others how to write poor code.
Oh, believe me I know pre allocation. I just did not not see the significance of pre allocation in the particular problem (except for few MILLI SECS!). I wrote a piece of code which he can inculcate with minimal modifications. So yeah thanks for suggestion.
"I just did not not see the significance of pre allocation in the particular problem"
Your assumption is that you only need to answer this one very specific question, and it is not your aim to provide advice or help the OP write better code. I don't think this is a useful approach:
  • preallocation is a good habit: the more beginners practice good habits the more comfortable they will be using them, the easier it will be for them to use them, and they will gain the benefits of using them.
  • preallocation covers realistic use cases: perhaps the question only shows ten iterations, but the real data contains six million elements. Questions often included simplified examples, and unless you possess a magic crystal ball you made some assumptions that may or may not be true.
  • preallocation allows the OP and also future readers to use your solution as a general solution: even if the OP really only has ten iterations, future readers might have ten million... and you could have easily helped them too. This is a public forum, so it helps to consider future readers with similar problems who will read this thread. Or what the OP might do tomorrow.
Adding a preallocated array takes minimal effort, as KSSV and my answers show.

Sign in to comment.

More Answers (2)

function xs = close_point(p0,p1,p2,p3)
t=0:0.1:1 ;
xs = zeros(length(t),1) ;
for i = 1:length(t)
b0=(1-t(i)).^2;
b1=2*t(i).*(1-t(i));
b2=t(i).^2;
x=b0*p0(1)+b1*p1(1)+b2*p2(1);
y=b0*p0(2)+b1*p1(2)+b2*p2(2);
xs(i)=sqrt((p3(1)-x).^2+(p3(2)-y).^2) ;
end
Just use indexing with a preallocated variable:
function out = close_point(p0,p1,p2,p3)
vec = 0:0.1:1;
out = nan(size(vec));
for k = 1:numel(vec);
t = vec(k);
b0=(1-t).^2;
b1=2*t.*(1-t);
b2=t.^2;
x=b0*p0(1)+b1*p1(1)+b2*p2(1);
y=b0*p0(2)+b1*p1(2)+b2*p2(2);
out(k) = sqrt((p3(1)-x).^2+(p3(2)-y).^2);
end
end

Categories

Community Treasure Hunt

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

Start Hunting!