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:
for loop

Subject: for loop

From: Tarik

Date: 13 May, 2013 13:34:07

Message: 1 of 5

hi...

it's really hard for me to explain what i want to calculate... but here is a part of my codes... when I write "n(9)"

ans =

     0 0 3
     0 3 0
     3 0 0

and if I cahange l as l=1:sqrt(ni) (it starts from 1)... now

ans =

     1 2 2
     2 1 2
     2 2 1

my problem is that both ans are in my expectation... I mean, for loop doesn't work well here... it looks that it stops after a solution... but why???

   for l=0:sqrt(ni)
         x=l;
         
       y=real(fix(sqrt(ni-x^2)));
       if fix(y)==y
       z=real(fix(sqrt(ni-x^2-y^2)));
       if fix(z)==z
       if ni==x^2+y^2+z^2;
       n=[x,y,z];
       
       result=unique(perms(n),'rows');
             return
              end
              end
              end
      end

Subject: for loop

From: Tarik

Date: 13 May, 2013 19:17:11

Message: 2 of 5

probably it is not understood properly...
my expectation is that this codes calculate n(9) for l=0 and =1(both) hence the result would be
 ans =
>
> 0 0 3
> 0 3 0
> 3 0 0
and
> ans =
>
> 1 2 2
> 2 1 2
> 2 2 1

but here I think for loop doesn't work properly
but where is the problem...
I hope someone can understand and help...
(sorry for this confusion)

"Tarik " <tarikak@gmail.com> wrote in message <kmqq4f$dpf$1@newscl01ah.mathworks.com>...
> hi...
>
> it's really hard for me to explain what i want to calculate... but here is a part of my codes... when I write "n(9)"
>
> ans =
>
> 0 0 3
> 0 3 0
> 3 0 0
>
> and if I cahange l as l=1:sqrt(ni) (it starts from 1)... now
>
> ans =
>
> 1 2 2
> 2 1 2
> 2 2 1
>
> my problem is that both ans are in my expectation... I mean, for loop doesn't work well here... it looks that it stops after a solution... but why???
>
> for l=0:sqrt(ni)
> x=l;
>
> y=real(fix(sqrt(ni-x^2)));
> if fix(y)==y
> z=real(fix(sqrt(ni-x^2-y^2)));
> if fix(z)==z
> if ni==x^2+y^2+z^2;
> n=[x,y,z];
>
> result=unique(perms(n),'rows');
> return
> end
> end
> end
> end

Subject: for loop

From: dpb

Date: 13 May, 2013 20:01:26

Message: 3 of 5

On 5/13/2013 2:17 PM, Tarik wrote:
> probably it is not understood properly...
> my expectation is that this codes calculate n(9) for l=0 and =1(both)
> hence the result would be ans =
>>
>> 0 0 3
>> 0 3 0
>> 3 0 0
> and
>> ans =
>>
>> 1 2 2
>> 2 1 2
>> 2 2 1
>
> but here I think for loop doesn't work properly
> but where is the problem...
> I hope someone can understand and help...
> (sorry for this confusion)
...

>> my problem is that both ans are in my expectation... I mean, for loop
>> doesn't work well here... it looks that it stops after a solution...
>> but why???
>>
>> for l=0:sqrt(ni)
>> x=l;
      y=real(fix(sqrt(ni-x^2)));
      if fix(y)==y
>> z=real(fix(sqrt(ni-x^2-y^2)));
        if fix(z)==z
          if ni==x^2+y^2+z^2;
>> n=[x,y,z];
            result=unique(perms(n),'rows');
            return
          end
>> end
>> end
>> end

Well, I've no idea what it is you're trying to describe as the result
but the reason the code will quit is the 'return' statement inside the
innermost if clause. If that IF... is ever satisfied, the RETURN will
then terminate the function at that point and (not surprisingly) return
to the calling routine or command line.

doc return

Perhaps you're looking for

doc break
doc continue

mayhaps???

--


--

Subject: for loop

From: dpb

Date: 13 May, 2013 20:03:14

Message: 4 of 5

On 5/13/2013 2:17 PM, Tarik wrote:
> probably it is not understood properly...
> my expectation is that this codes calculate n(9) for l=0 and =1(both)
> hence the result would be ans =
>>
>> 0 0 3
>> 0 3 0
>> 3 0 0
> and
>> ans =
>>
>> 1 2 2
>> 2 1 2
>> 2 2 1
>
> but here I think for loop doesn't work properly
> but where is the problem...
> I hope someone can understand and help...
> (sorry for this confusion)
...

>> my problem is that both ans are in my expectation... I mean, for loop
>> doesn't work well here... it looks that it stops after a solution...
>> but why???
>>
>> for l=0:sqrt(ni)
>> x=l;
      y=real(fix(sqrt(ni-x^2)));
      if fix(y)==y
>> z=real(fix(sqrt(ni-x^2-y^2)));
        if fix(z)==z
          if ni==x^2+y^2+z^2;
>> n=[x,y,z];
            result=unique(perms(n),'rows');
            return
          end
>> end
>> end
>> end

Well, I've no idea what it is you're trying to describe as the result
but the reason the code will quit is the 'return' statement inside the
innermost if clause. If that IF... is ever satisfied, the RETURN will
then terminate the function at that point and (not surprisingly) return
to the calling routine or command line.

doc return

Perhaps you're looking for

doc break
doc continue

mayhaps???

--


--

Subject: for loop

From: Tarik

Date: 13 May, 2013 20:37:20

Message: 5 of 5


Thank you...

result=unique(perms(n),'rows');

this is also problem :D

because even if I delete ifs, I have same problem...

to get all result, I have to write just perms(n), but this time some result is repeated as usual...


"Tarik " <tarikak@gmail.com> wrote in message <kmqq4f$dpf$1@newscl01ah.mathworks.com>...
> hi...
>
> it's really hard for me to explain what i want to calculate... but here is a part of my codes... when I write "n(9)"
>
> ans =
>
> 0 0 3
> 0 3 0
> 3 0 0
>
> and if I cahange l as l=1:sqrt(ni) (it starts from 1)... now
>
> ans =
>
> 1 2 2
> 2 1 2
> 2 2 1
>
> my problem is that both ans are in my expectation... I mean, for loop doesn't work well here... it looks that it stops after a solution... but why???
>
> for l=0:sqrt(ni)
> x=l;
>
> y=real(fix(sqrt(ni-x^2)));
> if fix(y)==y
> z=real(fix(sqrt(ni-x^2-y^2)));
> if fix(z)==z
> if ni==x^2+y^2+z^2;
> n=[x,y,z];
>
> result=unique(perms(n),'rows');
> return
> end
> end
> end
> end

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