Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Maxtrix generation with nested loops

Subject: Maxtrix generation with nested loops

From: Demetrius Salas

Date: 4 May, 2012 02:46:50

Message: 1 of 2

I am having a bit of a problem I am attempting to generate a matrix using a pair of while loops and then mesh plot that matrix. For some reason it seems that one of my variables doesn't change along with its increment and I can't for the life of me figure out why. Here is my code:
    mz=[]
    my2=[]
    i=1
    j=1
    xv1=x(i)
    yv1=y(j)
    y2=x.^2+y.^2
    while i<xmax+1
        while j<ymax+1
            yv2=y2(x(i),y(j))
            my2(end+1)=yv2
            j=j+1
        end
        mz(i,:)=my2
        i=i+1
    end
    mesh(mz)
end

The expected matrix is
     2 5 10 17 26 37 50 65 82 101
     5 8 13 20 29 40 53 68 85 104
    10 13 18 25 34 45 58 73 90 109
    17 20 25 32 41 52 65 80 97 116
    26 29 34 41 50 61 74 89 106 125
    37 40 45 52 61 72 85 100 117 136
    50 53 58 65 74 85 98 113 130 149
    65 68 73 80 89 100 113 128 145 164
    82 85 90 97 106 117 130 145 162 181
   101 104 109 116 125 136 149 164 181 200

The matrix I am getting is
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200
     2 8 18 32 50 72 98 128 162 200

It looks to me like the x value isn't changing for i

Thanks in advance.

Subject: Maxtrix generation with nested loops

From: Roger Stafford

Date: 4 May, 2012 05:06:23

Message: 2 of 2

"Demetrius Salas" wrote in message <jnvfuq$p3$1@newscl01ah.mathworks.com>...
> I am having a bit of a problem I am attempting to generate a matrix using a pair of while loops and then mesh plot that matrix. For some reason it seems that one of my variables doesn't change along with its increment and I can't for the life of me figure out why. .......
- - - - - - - - - -
  Based on what you are showing us, it looks as if you intended 'my2' and 'j' to be reset to [] and 1, respectively, each time the inner while-loop starts, but their initialization is actually outside the outer while-loop where initialization can happen only once. That would cause 'my2' to become longer and longer with each trip through the outer loop. The final 'mz' array would therefore be much wider than you show - I would guess 10 by 100.

  What is further puzzling is that the array y2=x.^2+y.^2 seems to already be the matrix you say you are expecting. What is the purpose of the further computation?

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us