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:
j not equal to i

Subject: j not equal to i

From: Ender

Date: 22 Jan, 2010 16:34:04

Message: 1 of 10

I am trying to make a summation that goes from j=1 to n when j does not equal i. for some input. Is there an easier way to code this in MATLAB without breaking the summation into j=1 to j-1 + j=j+1 to n. I am trying to code the Jacobi Iterative and the summation is a part of the code.

Any suggestions would be appreciated.

--Ender--

Subject: j not equal to i

From: Yi Cao

Date: 22 Jan, 2010 16:45:05

Message: 2 of 10

"Ender " <jr147@msstate.edu> wrote in message <hjck1s$25s$1@fred.mathworks.com>...
> I am trying to make a summation that goes from j=1 to n when j does not equal i. for some input. Is there an easier way to code this in MATLAB without breaking the summation into j=1 to j-1 + j=j+1 to n. I am trying to code the Jacobi Iterative and the summation is a part of the code.
>
> Any suggestions would be appreciated.
>
> --Ender--

You can do like the follows:

for j=1:n
   if j==i
      continue;
   end
   % do the rest you wish to in the loop.
end

HTH
Yi

Subject: j not equal to i

From: ImageAnalyst

Date: 22 Jan, 2010 16:53:07

Message: 3 of 10

 --Ender--
I thought of these two ways off the top of my head.
They're not necessarily better than splitting it up into two for loops
which is I think what you were hinting at.

j=5;
% Method 1
for k = [1:(j-1), (j+1):10]
k % Show index in command window.
end
% Method 2
for m = 1:10
if m ~= j
m % Show index in command window.
end
end

Subject: j not equal to i

From: Ender

Date: 22 Jan, 2010 18:38:08

Message: 4 of 10

Thanks, I will try your example.

 In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.

Here is my code:

% Set the number of iterations to perform
N=100;

% Set the Tolerance
TOL=10e-3;

% Initial Vector
XO=[0;0;0];

% The Matrix A
A=[3,-1,1;3,6,2;3,3,7];

% The Matrix b
b=[1;0;4];

% % Returns the two integers in two separate variables.
[n,n]=size(A);

% Step 1
k=1:N;

% Step 2
while (k <= N)
      % initialize s1a + s1b
      s1a=0;
      s1b=0;
      % Step 3
      for i=1:n
          
          % To account for not using the j=i in the algorithm
          for j=1:i-1
              s1a=s1a+(A(i,j))*XO(j)+b(i);
          end
          for j=i+1:n
              s1b=s1b+(A(i,j))*XO(j)+b(i);
          end
          
          % Add the sections before & after j not equal to i
          sT=s1a+s1b;
         
          x(i)=-sT/A(i,i);
              
          % Step 4
          if abs(x(i)-XO)<TOL
             display(' The Procedure was successful')
             % Output x
             x(i);
             % Stop the Algorithm
             return
          end
      end
         % Step 5
         k=k+1;
         

      % Step 6
      % Update XO
      for i=1:n
          XO(i)=x(i);
      end
end


% Step 7
% Ouput to screen
display('Maximum number of iterations exceeded')
                
            

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc89418d@l11g2000yqb.googlegroups.com>...
> --Ender--
> I thought of these two ways off the top of my head.
> They're not necessarily better than splitting it up into two for loops
> which is I think what you were hinting at.
>
> j=5;
> % Method 1
> for k = [1:(j-1), (j+1):10]
> k % Show index in command window.
> end
> % Method 2
> for m = 1:10
> if m ~= j
> m % Show index in command window.
> end
> end
>

Subject: j not equal to i

From: Nathan

Date: 22 Jan, 2010 19:23:52

Message: 5 of 10

On Jan 22, 10:38 am, "Ender " <jr...@msstate.edu> wrote:
> Thanks, I will try your example.
>
>  In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.
>
> Here is my code:
>
> % Set the number of iterations to perform
> N=100;
>
> % Set the Tolerance
> TOL=10e-3;
>
> % Initial Vector
> XO=[0;0;0];
>
> % The Matrix A
> A=[3,-1,1;3,6,2;3,3,7];
>
> % The Matrix b
> b=[1;0;4];
>
> % % Returns the two integers in two separate variables.
> [n,n]=size(A);
>
> % Step 1
> k=1:N;
>
> % Step 2
> while (k <= N)
>       % initialize s1a + s1b
>       s1a=0;
>       s1b=0;
>       % Step 3
>       for i=1:n
>
>           % To account for not using the j=i in the algorithm
>           for j=1:i-1
>               s1a=s1a+(A(i,j))*XO(j)+b(i);
>           end
>           for j=i+1:n
>               s1b=s1b+(A(i,j))*XO(j)+b(i);
>           end
>
>           % Add the sections before & after j not equal to i
>           sT=s1a+s1b;
>
>           x(i)=-sT/A(i,i);
>
>           % Step 4
>           if abs(x(i)-XO)<TOL
>              display(' The Procedure was successful')
>              % Output x
>              x(i);
>              % Stop the Algorithm
>              return
>           end
>       end      
>          % Step 5
>          k=k+1;
>
>       % Step 6
>       % Update XO
>       for i=1:n
>           XO(i)=x(i);
>       end
> end  
>
> % Step 7
> % Ouput to screen
> display('Maximum number of iterations exceeded')
>
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc894...@l11g2000yqb.googlegroups.com>...
> >  --Ender--
> > I thought of these two ways off the top of my head.
> > They're not necessarily better than splitting it up into two for loops
> > which is I think what you were hinting at.
>
> > j=5;
> > % Method 1
> > for k = [1:(j-1), (j+1):10]
> >    k % Show index in command window.
> > end
> > % Method 2
> > for m = 1:10
> >    if m ~= j
> >            m % Show index in command window.
> >    end
> > end
>
>

Subject: j not equal to i

From: Nathan

Date: 22 Jan, 2010 19:25:27

Message: 6 of 10

On Jan 22, 10:38 am, "Ender " <jr...@msstate.edu> wrote:
> Thanks, I will try your example.
>
>  In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.
>
> Here is my code:
>
> % Set the number of iterations to perform
> N=100;
>
> % Set the Tolerance
> TOL=10e-3;
>
> % Initial Vector
> XO=[0;0;0];
>
> % The Matrix A
> A=[3,-1,1;3,6,2;3,3,7];
>
> % The Matrix b
> b=[1;0;4];
>
> % % Returns the two integers in two separate variables.
> [n,n]=size(A);
>
> % Step 1
> k=1:N;
>
> % Step 2
> while (k <= N)
>       % initialize s1a + s1b
>       s1a=0;
>       s1b=0;
>       % Step 3
>       for i=1:n
>
>           % To account for not using the j=i in the algorithm
>           for j=1:i-1
>               s1a=s1a+(A(i,j))*XO(j)+b(i);
>           end
>           for j=i+1:n
>               s1b=s1b+(A(i,j))*XO(j)+b(i);
>           end
>
>           % Add the sections before & after j not equal to i
>           sT=s1a+s1b;
>
>           x(i)=-sT/A(i,i);
>
>           % Step 4
>           if abs(x(i)-XO)<TOL
>              display(' The Procedure was successful')
>              % Output x
>              x(i);
>              % Stop the Algorithm
>              return
>           end
>       end      
>          % Step 5
>          k=k+1;
>
>       % Step 6
>       % Update XO
>       for i=1:n
>           XO(i)=x(i);
>       end
> end  
>
> % Step 7
> % Ouput to screen
> display('Maximum number of iterations exceeded')
>
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc894...@l11g2000yqb.googlegroups.com>...
> >  --Ender--
> > I thought of these two ways off the top of my head.
> > They're not necessarily better than splitting it up into two for loops
> > which is I think what you were hinting at.
>
> > j=5;
> > % Method 1
> > for k = [1:(j-1), (j+1):10]
> >    k % Show index in command window.
> > end
> > % Method 2
> > for m = 1:10
> >    if m ~= j
> >            m % Show index in command window.
> >    end
> > end
>
>

Two lines of code make your loop end early:

k=1:N;
AND
k=k+1;

The first line is an array from 1 to 100.
The second line shown adds 1 to every element of k.
The while loop tests to see if EVERY element in k is less than or
equal to N.
When you add 1 to k, it ranges from 2 to 101. 101 is not less than
100, therefore it exits.

-Nathan

Subject: j not equal to i

From: Ender

Date: 22 Jan, 2010 21:29:20

Message: 7 of 10

How do I fix this? I had read in the MATLAB help that while adds 1 to the element when doing a while limit. (e.g. eps example) So I tried commenting out the k+1 but this put the code into an infinite loop. I don't think I am supposed to comment out the k+1, but after reading your post how could I tell MATLAB to not add elements to the k vector?

--Ender--


Nathan <ngreco32@gmail.com> wrote in message <32ce2567-140e-46f3-93c8-88c6df54643e@l11g2000yqb.googlegroups.com>...
> On Jan 22, 10:38 am, "Ender " <jr...@msstate.edu> wrote:
> > Thanks, I will try your example.
> >
> >  In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.
> >
> > Here is my code:
> >
> > % Set the number of iterations to perform
> > N=100;
> >
> > % Set the Tolerance
> > TOL=10e-3;
> >
> > % Initial Vector
> > XO=[0;0;0];
> >
> > % The Matrix A
> > A=[3,-1,1;3,6,2;3,3,7];
> >
> > % The Matrix b
> > b=[1;0;4];
> >
> > % % Returns the two integers in two separate variables.
> > [n,n]=size(A);
> >
> > % Step 1
> > k=1:N;
> >
> > % Step 2
> > while (k <= N)
> >       % initialize s1a + s1b
> >       s1a=0;
> >       s1b=0;
> >       % Step 3
> >       for i=1:n
> >
> >           % To account for not using the j=i in the algorithm
> >           for j=1:i-1
> >               s1a=s1a+(A(i,j))*XO(j)+b(i);
> >           end
> >           for j=i+1:n
> >               s1b=s1b+(A(i,j))*XO(j)+b(i);
> >           end
> >
> >           % Add the sections before & after j not equal to i
> >           sT=s1a+s1b;
> >
> >           x(i)=-sT/A(i,i);
> >
> >           % Step 4
> >           if abs(x(i)-XO)<TOL
> >              display(' The Procedure was successful')
> >              % Output x
> >              x(i);
> >              % Stop the Algorithm
> >              return
> >           end
> >       end      
> >          % Step 5
> >          k=k+1;
> >
> >       % Step 6
> >       % Update XO
> >       for i=1:n
> >           XO(i)=x(i);
> >       end
> > end  
> >
> > % Step 7
> > % Ouput to screen
> > display('Maximum number of iterations exceeded')
> >
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc894...@l11g2000yqb.googlegroups.com>...
> > >  --Ender--
> > > I thought of these two ways off the top of my head.
> > > They're not necessarily better than splitting it up into two for loops
> > > which is I think what you were hinting at.
> >
> > > j=5;
> > > % Method 1
> > > for k = [1:(j-1), (j+1):10]
> > >    k % Show index in command window.
> > > end
> > > % Method 2
> > > for m = 1:10
> > >    if m ~= j
> > >            m % Show index in command window.
> > >    end
> > > end
> >
> >
>
> Two lines of code make your loop end early:
>
> k=1:N;
> AND
> k=k+1;
>
> The first line is an array from 1 to 100.
> The second line shown adds 1 to every element of k.
> The while loop tests to see if EVERY element in k is less than or
> equal to N.
> When you add 1 to k, it ranges from 2 to 101. 101 is not less than
> 100, therefore it exits.
>
> -Nathan

Subject: j not equal to i

From: Nathan

Date: 22 Jan, 2010 21:32:08

Message: 8 of 10

On Jan 22, 1:29 pm, "Ender " <jr...@msstate.edu> wrote:
> How do I fix this? I had read in the MATLAB help that while adds 1 to the element when doing a while limit. (e.g. eps example) So I tried commenting out the k+1 but this put the code into an infinite loop. I don't think I am supposed to comment out the k+1, but after reading your post how could I tell MATLAB to not add elements to the k vector?
>
> --Ender--
>
> Nathan <ngrec...@gmail.com> wrote in message <32ce2567-140e-46f3-93c8-88c6df546...@l11g2000yqb.googlegroups.com>...
> > On Jan 22, 10:38 am, "Ender " <jr...@msstate.edu> wrote:
> > > Thanks, I will try your example.
>
> > >  In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.
>
> > > Here is my code:
>
> > > % Set the number of iterations to perform
> > > N=100;
>
> > > % Set the Tolerance
> > > TOL=10e-3;
>
> > > % Initial Vector
> > > XO=[0;0;0];
>
> > > % The Matrix A
> > > A=[3,-1,1;3,6,2;3,3,7];
>
> > > % The Matrix b
> > > b=[1;0;4];
>
> > > % % Returns the two integers in two separate variables.
> > > [n,n]=size(A);
>
> > > % Step 1
> > > k=1:N;
>
> > > % Step 2
> > > while (k <= N)
> > >       % initialize s1a + s1b
> > >       s1a=0;
> > >       s1b=0;
> > >       % Step 3
> > >       for i=1:n
>
> > >           % To account for not using the j=i in the algorithm
> > >           for j=1:i-1
> > >               s1a=s1a+(A(i,j))*XO(j)+b(i);
> > >           end
> > >           for j=i+1:n
> > >               s1b=s1b+(A(i,j))*XO(j)+b(i);
> > >           end
>
> > >           % Add the sections before & after j not equal to i
> > >           sT=s1a+s1b;
>
> > >           x(i)=-sT/A(i,i);
>
> > >           % Step 4
> > >           if abs(x(i)-XO)<TOL
> > >              display(' The Procedure was successful')
> > >              % Output x
> > >              x(i);
> > >              % Stop the Algorithm
> > >              return
> > >           end
> > >       end      
> > >          % Step 5
> > >          k=k+1;
>
> > >       % Step 6
> > >       % Update XO
> > >       for i=1:n
> > >           XO(i)=x(i);
> > >       end
> > > end  
>
> > > % Step 7
> > > % Ouput to screen
> > > display('Maximum number of iterations exceeded')
>
> > > ImageAnalyst <imageanal...@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc894...@l11g2000yqb.googlegroups.com>...
> > > >  --Ender--
> > > > I thought of these two ways off the top of my head.
> > > > They're not necessarily better than splitting it up into two for loops
> > > > which is I think what you were hinting at.
>
> > > > j=5;
> > > > % Method 1
> > > > for k = [1:(j-1), (j+1):10]
> > > >    k % Show index in command window.
> > > > end
> > > > % Method 2
> > > > for m = 1:10
> > > >    if m ~= j
> > > >            m % Show index in command window.
> > > >    end
> > > > end
>
> > Two lines of code make your loop end early:
>
> > k=1:N;
> > AND
> > k=k+1;
>
> > The first line is an array from 1 to 100.
> > The second line shown adds 1 to every element of k.
> > The while loop tests to see if EVERY element in k is less than or
> > equal to N.
> > When you add 1 to k, it ranges from 2 to 101. 101 is not less than
> > 100, therefore it exits.
>
> > -Nathan
>
>

You don't need to initialize k with 1:N. Just use 1.
(Why would k need to be an array anyways?)

With k = 1, initially, through the loop it will increment to
eventually reach 100, as you wished.

-Nathan

Subject: j not equal to i

From: Ender

Date: 22 Jan, 2010 21:54:05

Message: 9 of 10

How do I fix this? I had read in the MATLAB help that while adds 1 to the element when doing a while limit. (e.g. eps example) So I tried commenting out the k+1 but this put the code into an infinite loop. I don't think I am supposed to comment out the k+1, but after reading your post how could I tell MATLAB to not add elements to the k vector?

--Ender--


Nathan <ngreco32@gmail.com> wrote in message <32ce2567-140e-46f3-93c8-88c6df54643e@l11g2000yqb.googlegroups.com>...
> On Jan 22, 10:38 am, "Ender " <jr...@msstate.edu> wrote:
> > Thanks, I will try your example.
> >
> >  In my current code I cannot figure out why MATLAB is not continuing my while loop. At the end of the code and outside of the while loop I wrote a code to display that the maximum number of iterations have been exceeded. However, my code does this at k=2 instead of k=100. I think it is something simple that I am leaving out or forgetting, but I can not figure out what it is.
> >
> > Here is my code:
> >
> > % Set the number of iterations to perform
> > N=100;
> >
> > % Set the Tolerance
> > TOL=10e-3;
> >
> > % Initial Vector
> > XO=[0;0;0];
> >
> > % The Matrix A
> > A=[3,-1,1;3,6,2;3,3,7];
> >
> > % The Matrix b
> > b=[1;0;4];
> >
> > % % Returns the two integers in two separate variables.
> > [n,n]=size(A);
> >
> > % Step 1
> > k=1:N;
> >
> > % Step 2
> > while (k <= N)
> >       % initialize s1a + s1b
> >       s1a=0;
> >       s1b=0;
> >       % Step 3
> >       for i=1:n
> >
> >           % To account for not using the j=i in the algorithm
> >           for j=1:i-1
> >               s1a=s1a+(A(i,j))*XO(j)+b(i);
> >           end
> >           for j=i+1:n
> >               s1b=s1b+(A(i,j))*XO(j)+b(i);
> >           end
> >
> >           % Add the sections before & after j not equal to i
> >           sT=s1a+s1b;
> >
> >           x(i)=-sT/A(i,i);
> >
> >           % Step 4
> >           if abs(x(i)-XO)<TOL
> >              display(' The Procedure was successful')
> >              % Output x
> >              x(i);
> >              % Stop the Algorithm
> >              return
> >           end
> >       end      
> >          % Step 5
> >          k=k+1;
> >
> >       % Step 6
> >       % Update XO
> >       for i=1:n
> >           XO(i)=x(i);
> >       end
> > end  
> >
> > % Step 7
> > % Ouput to screen
> > display('Maximum number of iterations exceeded')
> >
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <2047bb41-198b-4a35-9003-bc68cc894...@l11g2000yqb.googlegroups.com>...
> > >  --Ender--
> > > I thought of these two ways off the top of my head.
> > > They're not necessarily better than splitting it up into two for loops
> > > which is I think what you were hinting at.
> >
> > > j=5;
> > > % Method 1
> > > for k = [1:(j-1), (j+1):10]
> > >    k % Show index in command window.
> > > end
> > > % Method 2
> > > for m = 1:10
> > >    if m ~= j
> > >            m % Show index in command window.
> > >    end
> > > end
> >
> >
>
> Two lines of code make your loop end early:
>
> k=1:N;
> AND
> k=k+1;
>
> The first line is an array from 1 to 100.
> The second line shown adds 1 to every element of k.
> The while loop tests to see if EVERY element in k is less than or
> equal to N.
> When you add 1 to k, it ranges from 2 to 101. 101 is not less than
> 100, therefore it exits.
>
> -Nathan

Subject: j not equal to i

From: Nathan

Date: 22 Jan, 2010 22:11:48

Message: 10 of 10

Did you bother reading my post?

Do you need k to be a vector?
(What use is it as a vector, when it seems to only be used as a
counting variable?)

If you don't need it to be a vector, don't create it as a vector.
As I mentioned:
Instead of writing "k=1:N", replace that with "k=1". You won't have to
touch the "k=k+1".
Doing this will let your code iterate through the while loop, until k
finally reaches 100.
Note that doing this will make k a single element, not a vector of
multiple elements. This is what I think you are looking for.

-Nathan

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