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:
Increment & decrement Loop in MATLAB

Subject: Increment & decrement Loop in MATLAB

From: Lara

Date: 23 Feb, 2006 00:55:05

Message: 1 of 9

Hello every body,

Can I do increment & decrement for counters of loop in 2D array ???

ex:

for i=1:5

   for j=1:6

         A[i,j]=1;

I want to do the follow :
first, increment only j --> 1,1 1,2 1,3 1,4 1,5 1,6
then increment only i --> 2,6 3,6 4,6 5,6
after that decrement only j --> 5,5 5,4 5,3 5,2 5,1
finally, decrement only i --> 4,1 3,1 2,1 1,1

i.e I want to check these elements in array from (1,1) to (2,1)

1,1 1,2 1,3 1,4 1,5 1,6
2,1 2,6
3,1 3,6
4,1 4,6
5,1 5,2 5,3 5,4 5,5 5,6

Can i do this in two loop only , one for (i) counter and other for (j)
counter ??
OR, what's the best method to do that ??

thanks in advance,

Lara,

Subject: Increment & decrement Loop in MATLAB

From: Michael Wild

Date: 23 Feb, 2006 10:29:03

Message: 2 of 9

Lara wrote:
> Hello every body,
>
> Can I do increment & decrement for counters of loop in 2D array ???
>
> ex:
>
> for i=1:5
>
> for j=1:6
>
> A[i,j]=1;
>
> I want to do the follow :
> first, increment only j --> 1,1 1,2 1,3 1,4 1,5 1,6
> then increment only i --> 2,6 3,6 4,6 5,6
> after that decrement only j --> 5,5 5,4 5,3 5,2 5,1
> finally, decrement only i --> 4,1 3,1 2,1 1,1
>
> i.e I want to check these elements in array from (1,1) to (2,1)
>
> 1,1 1,2 1,3 1,4 1,5 1,6
> 2,1 2,6
> 3,1 3,6
> 4,1 4,6
> 5,1 5,2 5,3 5,4 5,5 5,6
>
> Can i do this in two loop only , one for (i) counter and other for (j)
> counter ??
> OR, what's the best method to do that ??
>
> thanks in advance,
>
> Lara,
>


depends on what you want do do. usually these kind of for loops are a
bad idea, since they are slow. to take your example, one of the
solutions could be:

A = zeros(5,6);
A(:,1) = 1;
A(2:end-1,end) = 1;
A(1,:) = 1;
A(end,1) = 1;

the matlab language does not allow to change the loop counting variable
(you can change it, but it is reset to consecutive value after each
loop). if you want control like that, use while loops and do your own
book-keeping.

michael

Subject: Increment & decrement Loop in MATLAB

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 23 Feb, 2006 09:46:55

Message: 3 of 9

In article <1140684905.214396.235960@g47g2000cwa.googlegroups.com>, "Lara"
<the_brat_taz@yahoo.com> wrote:

> Hello every body,
>
> Can I do increment & decrement for counters of loop in 2D array ???
>
> ex:
>
> for i=1:5
>
> for j=1:6
>
> A[i,j]=1;
>
> I want to do the follow :
> first, increment only j --> 1,1 1,2 1,3 1,4 1,5 1,6
> then increment only i --> 2,6 3,6 4,6 5,6
> after that decrement only j --> 5,5 5,4 5,3 5,2 5,1
> finally, decrement only i --> 4,1 3,1 2,1 1,1
>
> i.e I want to check these elements in array from (1,1) to (2,1)
>
> 1,1 1,2 1,3 1,4 1,5 1,6
> 2,1 2,6
> 3,1 3,6
> 4,1 4,6
> 5,1 5,2 5,3 5,4 5,5 5,6
>
> Can i do this in two loop only , one for (i) counter and other for (j)
> counter ??
> OR, what's the best method to do that ??
>
> thanks in advance,
>
> Lara,
-------------------
  You want some sort of for-loop that causes the i and j indices to vary
in the manner you have described? Try this:

i = 1; j = 1;
for k = 1:18
 % <= Check A(i,j) here
 if i==1 & j<6
  j = j + 1;
 elseif i<5 & j==6
  i = i + 1;
 elseif i==5 & j>1
  j = j - 1;
 else
  i = i - 1;
 end
end

If you just want to set A to 1 on these pairs, do this:

[j,i] = meshgrid(1:6,1:5);
i = i(:); j = j(:);
A = reshape((i==1)|(i==5)|(j==1)|(j==6),5,6);

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford

Subject: Increment & decrement Loop in MATLAB

From: Steve Amphlett

Date: 23 Feb, 2006 05:50:16

Message: 4 of 9

Michael Wild wrote:
>
>
> the matlab language does not allow to change the loop counting
> variable
> (you can change it, but it is reset to consecutive value after each
>
> loop). if you want control like that, use while loops and do your
> own
> book-keeping.

Just to clarify, Matlab does not have any notion of a "loop counting
variable". It has a <for> keyword that allows a variable to
take the values of successive columns of an array in a loop. These
are equally valid:

for n=1:n
% stuff
end

for n=[1 2 3 4]
% stuff
end

for n=[1:10 5 4 3 2 1]
% stuff
end

x=1:10;
for n=x
% stuff
end

for n=magic(5)
% stuff
end

Subject: Increment & decrement Loop in MATLAB

From: Steve Amphlett

Date: 23 Feb, 2006 05:52:43

Message: 5 of 9

Steve Amphlett wrote:
>
>
<snip>
 
> for n=1:n
> % stuff
> end

A slight typo here (should have been for n=1:4), but still valid
anyway and if n is an integer, there will be no side effects.

Subject: Increment & decrement Loop in MATLAB

From: Peter Boettcher

Date: 23 Feb, 2006 08:46:08

Message: 6 of 9

"Lara" <the_brat_taz@yahoo.com> writes:

> Hello every body,
>
> Can I do increment & decrement for counters of loop in 2D array ???
>
> ex:
>
> for i=1:5
>
> for j=1:6
>
> A[i,j]=1;
>
> I want to do the follow :
> first, increment only j --> 1,1 1,2 1,3 1,4 1,5 1,6
> then increment only i --> 2,6 3,6 4,6 5,6
> after that decrement only j --> 5,5 5,4 5,3 5,2 5,1
> finally, decrement only i --> 4,1 3,1 2,1 1,1
>
> i.e I want to check these elements in array from (1,1) to (2,1)
>
> 1,1 1,2 1,3 1,4 1,5 1,6
> 2,1 2,6
> 3,1 3,6
> 4,1 4,6
> 5,1 5,2 5,3 5,4 5,5 5,6
>
> Can i do this in two loop only , one for (i) counter and other for (j)
> counter ??
> OR, what's the best method to do that ??

You might be best served by creating a list of all the 2D coordinates
you want to check. You can construct that list by hand or by lines of
code:

ci = [ones(1,6) 2:4 5*ones(1,5) 5:-1:2];
cj = [1:6 6*ones(1,3) 6:-1:1 ones(1,3)];

for idx=1:length(ci)
  A(ci(idx), cj(idx)) = 1;
end

You can also use this list vectorized:

A = zeros(5,6);
A(sub2ind(size(A), ci, cj)) = 1;


--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: Increment & decrement Loop in MATLAB

From: Steven Lord

Date: 23 Feb, 2006 09:20:40

Message: 7 of 9


"Michael Wild" <themiwi@student.ethz.ch> wrote in message
news:43fd805f$1@news1.ethz.ch...

*snip question about creating a 'frame' of ones in a matrix*

> depends on what you want do do. usually these kind of for loops are a bad
> idea, since they are slow. to take your example, one of the solutions
> could be:
>
> A = zeros(5,6);
> A(:,1) = 1;
> A(2:end-1,end) = 1;
> A(1,:) = 1;
> A(end,1) = 1;

Actually, two minor nitpicks. I think you intended the last command to be:

A(end, :) = 1;

as otherwise most of the elements in the last row don't get changed to 1.
The second minor nitpick is with the third line. There's no need to tell
MATLAB to start with the second element in the last column and proceed until
just before the last row. That line can be:

A(:, end) = 1;

Sure, those four indexing lines set the corner elements to be 1 twice, but
that's okay. Overwriting a 1 with a 1 does no harm; if you were performing
calculations and assigning values to those elements based on the results of
those calculations, of course, you'd need to be more careful to avoid
overlapping.

> the matlab language does not allow to change the loop counting variable
> (you can change it, but it is reset to consecutive value after each loop)

To be precise, the loop variable takes on the next column of the expression
after the "for i = " after each iteration through the loop. There's no
requirement that the expression consist of consecutive values, but that's
most often the case. You could loop over (some of) the prime numbers, for
instance:

for k=[2 3 5 7 11 13 17 19]
% Do something
end

> if you want control like that, use while loops and do your own
> book-keeping.

Yes.

--
Steve Lord
slord@mathworks.com

Subject: Increment & decrement Loop in MATLAB

From: Michael Wild

Date: 23 Feb, 2006 15:57:44

Message: 8 of 9

Steven Lord wrote:
> "Michael Wild" <themiwi@student.ethz.ch> wrote in message
> news:43fd805f$1@news1.ethz.ch...
>
> *snip question about creating a 'frame' of ones in a matrix*
>
>
>>depends on what you want do do. usually these kind of for loops are a bad
>>idea, since they are slow. to take your example, one of the solutions
>>could be:
>>
>>A = zeros(5,6);
>>A(:,1) = 1;
>>A(2:end-1,end) = 1;
>>A(1,:) = 1;
>>A(end,1) = 1;
>
>
> Actually, two minor nitpicks. I think you intended the last command to be:
>
> A(end, :) = 1;
>
> as otherwise most of the elements in the last row don't get changed to 1.
> The second minor nitpick is with the third line. There's no need to tell
> MATLAB to start with the second element in the last column and proceed until
> just before the last row. That line can be:
>
> A(:, end) = 1;
>
> Sure, those four indexing lines set the corner elements to be 1 twice, but
> that's okay. Overwriting a 1 with a 1 does no harm; if you were performing
> calculations and assigning values to those elements based on the results of
> those calculations, of course, you'd need to be more careful to avoid
> overlapping.
>
>
>>the matlab language does not allow to change the loop counting variable
>>(you can change it, but it is reset to consecutive value after each loop)
>
>
> To be precise, the loop variable takes on the next column of the expression
> after the "for i = " after each iteration through the loop. There's no
> requirement that the expression consist of consecutive values, but that's
> most often the case. You could loop over (some of) the prime numbers, for
> instance:
>
> for k=[2 3 5 7 11 13 17 19]
> % Do something
> end
>
>
>>if you want control like that, use while loops and do your own
>>book-keeping.
>
>
> Yes.
>


thanks steven.

for the second correction: this is actually correct, but the error is in
the statements after the one you mention (as the righthand corner
elements should not be assigned to, at least from the example given).
the correct thing:

A = zeros(5,6);
A(:,1) = 1;
A(2:end-1,end) = 1;
A(1,1:end-1) = 1;
A(end,1:end-1) = 1;


michael

Subject: Increment & decrement Loop in MATLAB

From: Lara

Date: 23 Feb, 2006 08:22:16

Message: 9 of 9

Thanks for all

But I don't want to set A to 1 on these pairs
it's just an example..

I want to do this manner in any part of an array

first, increment only j
then increment only i
after that decrement only
finally, decrement only i

like what Roger Stafford provided

i = 1; j = 1;
for k = 1:18
 % <= Check A(i,j) here
 if i==1 & j<6
  j = j + 1;
 elseif i<5 & j==6
  i = i + 1;
 elseif i==5 & j>1
  j = j - 1;
 else
  i = i - 1;
 end
end

if you have another method , tell me please ..

Thanks for all again,
Regards,

Lara,

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