how to collect variables in an aray in for loop? (2024)

2 views (last 30 days)

Show older comments

Vishal Rajpurohit on 22 May 2018

  • Link

    Direct link to this question

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop

  • Link

    Direct link to this question

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop

Commented: Stephen23 on 23 May 2018

Accepted Answer: Maverick

Open in MATLAB Online

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?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Maverick on 22 May 2018

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#answer_321373

Open in MATLAB Online

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

Show 1 older commentHide 1 older comment

John D'Errico on 22 May 2018

Direct link to this comment

https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#comment_570639

  • Link

    Direct link to this comment

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#comment_570639

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.

Maverick on 23 May 2018

Direct link to this comment

https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#comment_570982

  • Link

    Direct link to this comment

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#comment_570982

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.

Stephen23 on 23 May 2018

Direct link to this comment

https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#comment_570986

  • Link

    Direct link to this comment

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#comment_570986

"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)

KSSV on 22 May 2018

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#answer_321369

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#answer_321369

Open in MATLAB Online

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Stephen23 on 22 May 2018

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#answer_321371

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/401937-how-to-collect-variables-in-an-aray-in-for-loop#answer_321371

Edited: Stephen23 on 22 May 2018

Open in MATLAB Online

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

https://www.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsMatrices and ArraysCreating and Concatenating Matrices

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

  • for loop
  • array

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


how to collect variables in an aray in for loop? (8)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

how to collect variables in an aray in for loop? (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6373

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.