Loop

Home Forums Matlab Loop

This topic contains 3 replies, has 2 voices, and was last updated by  cK 10 years, 8 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #182

    Anonymous

    With i=(nell/2+2):nell+1

    i =6 7 8 9

    in following loop
    for i=(nell/2+2):nell+1
    NN(i)=0;
    for j=(nelw/2+1):nelw+1

    NNP(i)=NN(i)+Y(j)*probxy(i,j)*b
    end
    end
    X1=(1:a:4);
    figure(2)
    plot(X1,NNP);
    I should get only 4 value of NNP
    But I am getting 11 value…..I am not able to understand th reason …Please guide

    #183

    cK
    Participant

    You might be having an older variable NNP already in workspace before you begin this loop. If the older variable is say 1×11 vector, the first 4 values will be overwritten with your loop, but the rest of the elements 5-11 will remain and the length of the array is still 1×11. You should clear and initialise the variables you plan to create inside loops.

    #184

    Anonymous

    @cK said:

    You might be having an older variable NNP already in workspace before you begin this loop. If the older variable is say 1×11 vector, the first 4 values will be overwritten with your loop, but the rest of the elements 5-11 will remain and the length of the array is still 1×11. You should clear and initialise the variables you plan to create inside loops.

    Yes! earlier variable N was repeated but I have checked carefully……not the case with variable NNP. It seem some mistake in loop only.In matrix probxy I need to select right half column one by one and calculate level crossing. I should get 4 such value for this case ,but
    NNP =

    0 0 0 0 0 0 0 -0.0020
    ??? Error using ==> plot
    Vectors must be the same lengths.

    Error in ==> fem_optimized at 384
    plot(X1,NNP);

    #185

    cK
    Participant

    Oops, you are using i as index, where i runs from 6 to 9. That means you are creating NNP(6), NNP(7)..NNP(9). You hence have 9 elements in NNP. When you do A(100) = 1, A(1)…A(99) will be initialised to zeros and A will be 1×100 vector. Better use another index instead of i, e.g. idx = 1:length(i), and use idx as loop index.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.