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:
nested loop trouble

Subject: nested loop trouble

From: rasika

Date: 31 Oct, 2011 04:22:08

Message: 1 of 15

Hello, I am trying a function where I will need the following kind of logic of nested loops of for and if else. Can you please let me know where I am going wrong, itsgiving out all zeros as values of a. it is not storing values in a and neither going inside the loop

function[a] = checkingtheloops(t)
for t=[0:1:184.7]'
    a=zeros(size(t));

        if t<=5
            temp = t(i);
            a = x*temp;
           fprintf('a');
        elseif(t>5 & t<=15)
                
                temp =t(i);
                a =x*temp;
                fprintf('a');
        elseif(t>15 & t<=60)
            temp = t(i);
            a =x*temp;
            fprintf('a');
        end
    end
end
                

Subject: nested loop trouble

From: dpb

Date: 31 Oct, 2011 04:29:41

Message: 2 of 15

On 10/30/2011 10:22 PM, rasika wrote:
> Hello, I am trying a function where I will need the following kind of
> logic of nested loops of for and if else. Can you please let me know
> where I am going wrong, itsgiving out all zeros as values of a. it is
> not storing values in a and neither going inside the loop
>
> function[a] = checkingtheloops(t)
> for t=[0:1:184.7]'

You passed t then wrote over it. Undoubtedly not what you intended.
Not to mention the problem of a floating point value for the upper limit
for a sequence of integers as specified by 0:1: ...

> a=zeros(size(t));

Remove the semicolon and test...probably not what you are expecting
here, either...

> if t<=5
> temp = t(i);

'i' is the imaginary 'i' in Matlab unless you overload the function
which you have not done.

It is best practice to not do so but use some other variable for loop
and other indices in Matlab. But, if you wish to reference the vector t
which was (I presume) being passed, you'll need to do the loop on an
indexing variable that is consistently named.

...

It looks as though the rest could be written w/o a loop anyway in Matlab...

--

Subject: nested loop trouble

From: rasika

Date: 31 Oct, 2011 06:10:26

Message: 3 of 15

Yes, I just need thiis kind of logic for more complicated function. in place of
a, i will be calling two other functions which depend on t..can you suggest me what else should I change?



dpb <none@non.net> wrote in message <j8l87o$bs8$1@speranza.aioe.org>...
> On 10/30/2011 10:22 PM, rasika wrote:
> > Hello, I am trying a function where I will need the following kind of
> > logic of nested loops of for and if else. Can you please let me know
> > where I am going wrong, itsgiving out all zeros as values of a. it is
> > not storing values in a and neither going inside the loop
> >
> > function[a] = checkingtheloops(t)
> > for t=[0:1:184.7]'
>
> You passed t then wrote over it. Undoubtedly not what you intended.
> Not to mention the problem of a floating point value for the upper limit
> for a sequence of integers as specified by 0:1: ...
>
> > a=zeros(size(t));
>
> Remove the semicolon and test...probably not what you are expecting
> here, either...
>
> > if t<=5
> > temp = t(i);
>
> 'i' is the imaginary 'i' in Matlab unless you overload the function
> which you have not done.
>
> It is best practice to not do so but use some other variable for loop
> and other indices in Matlab. But, if you wish to reference the vector t
> which was (I presume) being passed, you'll need to do the loop on an
> indexing variable that is consistently named.
>
> ...
>
> It looks as though the rest could be written w/o a loop anyway in Matlab...
>
> --

Subject: nested loop trouble

From: Lothar Schmidt

Date: 31 Oct, 2011 07:16:10

Message: 4 of 15

Am 31.10.2011 05:22, schrieb rasika:
> Hello, I am trying a function where I will need the following kind of
> logic of nested loops of for and if else. Can you please let me know
> where I am going wrong, itsgiving out all zeros as values of a. it is
> not storing values in a and neither going inside the loop
>
> function[a] = checkingtheloops(t)
> for t=[0:1:184.7]'

overwriting t might be not a good idea

> a=zeros(size(t));

a will be one single 0

>
> if t<=5
> temp = t(i);

i not defined so sqrt(-1) will be used as index

> a = x*temp;

x not defined

> fprintf('a');

ever read the the documentation of fprintf command?

> elseif(t>5 & t<=15)
> temp =t(i);

same problem as above

> a =x*temp;

same problem as above

> fprintf('a');

same problem as above

> elseif(t>15 & t<=60)
> temp = t(i);

same problem as above

> a =x*temp;

same problem as above

> fprintf('a');

same problem as above

> end
> end
> end

Subject: nested loop trouble

From: rasika

Date: 31 Oct, 2011 08:20:28

Message: 5 of 15

x will be passed in workspace. a does give out a column vector as the same size of t.
I have removed the fprintf and overwriting and index i... still all it gives out is a series of zeros for a..why does it not calculate the values?
Lothar Schmidt <vapooroop@gmx.net> wrote in message <j8lhvr$49f$1@news.albasani.net>...
> Am 31.10.2011 05:22, schrieb rasika:
> > Hello, I am trying a function where I will need the following kind of
> > logic of nested loops of for and if else. Can you please let me know
> > where I am going wrong, itsgiving out all zeros as values of a. it is
> > not storing values in a and neither going inside the loop
> >
> > function[a] = checkingtheloops(t)
> > for t=[0:1:184.7]'
>
> overwriting t might be not a good idea
>
> > a=zeros(size(t));
>
> a will be one single 0
>
> >
> > if t<=5
> > temp = t(i);
>
> i not defined so sqrt(-1) will be used as index
>
> > a = x*temp;
>
> x not defined
>
> > fprintf('a');
>
> ever read the the documentation of fprintf command?
>
> > elseif(t>5 & t<=15)
> > temp =t(i);
>
> same problem as above
>
> > a =x*temp;
>
> same problem as above
>
> > fprintf('a');
>
> same problem as above
>
> > elseif(t>15 & t<=60)
> > temp = t(i);
>
> same problem as above
>
> > a =x*temp;
>
> same problem as above
>
> > fprintf('a');
>
> same problem as above
>
> > end
> > end
> > end
>
>

Subject: nested loop trouble

From: rasika

Date: 31 Oct, 2011 08:29:10

Message: 6 of 15

function[a] = checkingtheloops(t)
%for t=[0:1:184]'
n=size(t);
% m=size(a);
% m=n;
a= zeros(n);
for b = 1:size(t)
        if t<=5
            
            a = t(b)*2;
           
        elseif(t>5 & t<=15)
                
                
                a = t(b)*3 ;
        elseif(t>15 & t<=60)
           
            a =t(b)*4
           
        end
end
end
                
       


"rasika " <n.rasika@gmail.com> wrote in message <j8lloc$5lt$1@newscl01ah.mathworks.com>...
> x will be passed in workspace. a does give out a column vector as the same size of t.
> I have removed the fprintf and overwriting and index i... still all it gives out is a series of zeros for a..why does it not calculate the values?
> Lothar Schmidt <vapooroop@gmx.net> wrote in message <j8lhvr$49f$1@news.albasani.net>...
> > Am 31.10.2011 05:22, schrieb rasika:
> > > Hello, I am trying a function where I will need the following kind of
> > > logic of nested loops of for and if else. Can you please let me know
> > > where I am going wrong, itsgiving out all zeros as values of a. it is
> > > not storing values in a and neither going inside the loop
> > >
> > > function[a] = checkingtheloops(t)
> > > for t=[0:1:184.7]'
> >
> > overwriting t might be not a good idea
> >
> > > a=zeros(size(t));
> >
> > a will be one single 0
> >
> > >
> > > if t<=5
> > > temp = t(i);
> >
> > i not defined so sqrt(-1) will be used as index
> >
> > > a = x*temp;
> >
> > x not defined
> >
> > > fprintf('a');
> >
> > ever read the the documentation of fprintf command?
> >
> > > elseif(t>5 & t<=15)
> > > temp =t(i);
> >
> > same problem as above
> >
> > > a =x*temp;
> >
> > same problem as above
> >
> > > fprintf('a');
> >
> > same problem as above
> >
> > > elseif(t>15 & t<=60)
> > > temp = t(i);
> >
> > same problem as above
> >
> > > a =x*temp;
> >
> > same problem as above
> >
> > > fprintf('a');
> >
> > same problem as above
> >
> > > end
> > > end
> > > end
> >
> >

Subject: nested loop trouble

From: Torsten

Date: 31 Oct, 2011 09:07:29

Message: 7 of 15

On 31 Okt., 09:29, "rasika " <n.ras...@gmail.com> wrote:
> function[a] = checkingtheloops(t)
> %for t=[0:1:184]'
> n=size(t);
> % m=size(a);
> % m=n;
> a= zeros(n);
> for b = 1:size(t)
>         if t<=5
>
>             a = t(b)*2;
>
>         elseif(t>5 & t<=15)
>
>                 a = t(b)*3  ;    
>         elseif(t>15 & t<=60)
>
>             a =t(b)*4
>
>         end
> end
> end
>
>
>
> "rasika " <n.ras...@gmail.com> wrote in message <j8lloc$5l...@newscl01ah.mathworks.com>...
> > x will be passed in workspace. a does give out a column vector as the same size of t.
> > I have removed the fprintf and overwriting and index i... still all it gives out is a series of zeros for a..why does it not calculate the values?
> > Lothar Schmidt <vapoor...@gmx.net> wrote in message <j8lhvr$49...@news.albasani.net>...
> > > Am 31.10.2011 05:22, schrieb rasika:
> > > > Hello, I am trying a function where I will need the following kind of
> > > > logic of nested loops of for and if else. Can you please let me know
> > > > where I am going wrong, itsgiving out all zeros as values of a. it is
> > > > not storing values in a and neither going inside the loop
>
> > > > function[a] = checkingtheloops(t)
> > > > for t=[0:1:184.7]'
>
> > > overwriting t might be not a good idea
>
> > > > a=zeros(size(t));
>
> > > a will be one single 0
>
> > > > if t<=5
> > > > temp = t(i);
>
> > > i not defined so sqrt(-1) will be used as index
>
> > > > a = x*temp;
>
> > > x not defined
>
> > > > fprintf('a');
>
> > > ever read the the documentation of fprintf command?
>
> > > > elseif(t>5 & t<=15)
> > > > temp =t(i);
>
> > > same problem as above
>
> > > > a =x*temp;
>
> > > same problem as above
>
> > > > fprintf('a');
>
> > > same problem as above
>
> > > > elseif(t>15 & t<=60)
> > > > temp = t(i);
>
> > > same problem as above
>
> > > > a =x*temp;
>
> > > same problem as above
>
> > > > fprintf('a');
>
> > > same problem as above
>
> > > > end
> > > > end
> > > > end- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

You always compare the _complete_ array t with numbers 5, 15 and 60,
but I guess you want to do it _componentwise_:

for b = 1:size(t)
         if t(b)<=5
             a = t(b)*2;
         elseif(t(b)>5 & t(b)<=15)
             a = t(b)*3 ;
         elseif(t(b)>15 & t(b)<=60)
             a =t(b)*4;
        end
end

Best wishes
Torsten.

Subject: nested loop trouble

From: Torsten

Date: 31 Oct, 2011 09:32:45

Message: 8 of 15

On 31 Okt., 10:07, Torsten <Torsten.Hen...@umsicht.fraunhofer.de>
wrote:
> On 31 Okt., 09:29, "rasika " <n.ras...@gmail.com> wrote:
>
>
>
>
>
> > function[a] = checkingtheloops(t)
> > %for t=[0:1:184]'
> > n=size(t);
> > % m=size(a);
> > % m=n;
> > a= zeros(n);
> > for b = 1:size(t)
> >         if t<=5
>
> >             a = t(b)*2;
>
> >         elseif(t>5 & t<=15)
>
> >                 a = t(b)*3  ;    
> >         elseif(t>15 & t<=60)
>
> >             a =t(b)*4
>
> >         end
> > end
> > end
>
> > "rasika " <n.ras...@gmail.com> wrote in message <j8lloc$5l...@newscl01ah.mathworks.com>...
> > > x will be passed in workspace. a does give out a column vector as the same size of t.
> > > I have removed the fprintf and overwriting and index i... still all it gives out is a series of zeros for a..why does it not calculate the values?
> > > Lothar Schmidt <vapoor...@gmx.net> wrote in message <j8lhvr$49...@news.albasani.net>...
> > > > Am 31.10.2011 05:22, schrieb rasika:
> > > > > Hello, I am trying a function where I will need the following kind of
> > > > > logic of nested loops of for and if else. Can you please let me know
> > > > > where I am going wrong, itsgiving out all zeros as values of a. it is
> > > > > not storing values in a and neither going inside the loop
>
> > > > > function[a] = checkingtheloops(t)
> > > > > for t=[0:1:184.7]'
>
> > > > overwriting t might be not a good idea
>
> > > > > a=zeros(size(t));
>
> > > > a will be one single 0
>
> > > > > if t<=5
> > > > > temp = t(i);
>
> > > > i not defined so sqrt(-1) will be used as index
>
> > > > > a = x*temp;
>
> > > > x not defined
>
> > > > > fprintf('a');
>
> > > > ever read the the documentation of fprintf command?
>
> > > > > elseif(t>5 & t<=15)
> > > > > temp =t(i);
>
> > > > same problem as above
>
> > > > > a =x*temp;
>
> > > > same problem as above
>
> > > > > fprintf('a');
>
> > > > same problem as above
>
> > > > > elseif(t>15 & t<=60)
> > > > > temp = t(i);
>
> > > > same problem as above
>
> > > > > a =x*temp;
>
> > > > same problem as above
>
> > > > > fprintf('a');
>
> > > > same problem as above
>
> > > > > end
> > > > > end
> > > > > end- Zitierten Text ausblenden -
>
> > - Zitierten Text anzeigen -
>
> You always compare the _complete_ array  t with numbers 5, 15 and 60,
> but I guess you want to do it _componentwise_:
>
> for b = 1:size(t)
>          if t(b)<=5
>              a = t(b)*2;
>          elseif(t(b)>5 & t(b)<=15)
>              a = t(b)*3  ;
>          elseif(t(b)>15 & t(b)<=60)
>              a =t(b)*4;
>         end
> end
>
> Best wishes
> Torsten.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

I think even this way:

for b = 1:size(t)
          if t(b)<=5
              a(b) = t(b)*2;
          elseif(t(b)>5 & t(b)<=15)
              a(b) = t(b)*3 ;
          elseif(t(b)>15 & t(b)<=60)
              a(b) =t(b)*4;
         end
 end

Best wishes
Torsten.

Subject: nested loop trouble

From: Steven_Lord

Date: 31 Oct, 2011 13:32:51

Message: 9 of 15



"rasika " <n.rasika@gmail.com> wrote in message
news:j8lloc$5lt$1@newscl01ah.mathworks.com...
> x will be passed in workspace. a does give out a column vector as the same
> size of t.
> I have removed the fprintf and overwriting and index i... still all it
> gives out is a series of zeros for a..why does it not calculate the
> values?

When you use:

if vectorCondition
    % do something
end

the condition will be satisfied, and the code for which "% do something" in
my example is a placeholder, will be executed only if vectorCondition is
nonempty and ALL of the elements of vectorCondition are true.

http://www.mathworks.com/help/techdoc/ref/if.html

"An evaluated expression is true when the result is nonempty and contains
all nonzero elements (logical or real numeric). Otherwise, the expression is
false."

Since not ALL of the elements of (t<=5) are true, your first IF condition is
not satisfied. Neither are any of the conditions on your ELSEIF blocks.

I recommend using logical indexing instead.

http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: nested loop trouble

From: rasika

Date: 1 Nov, 2011 02:24:27

Message: 10 of 15

Thanks Torsten,
 Will try that

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <479512c0-9d5d-48b2-8d64-6d0fe93d90a0@q13g2000vbd.googlegroups.com>...
> On 31 Okt., 10:07, Torsten <Torsten.Hen...@umsicht.fraunhofer.de>
> wrote:
> > On 31 Okt., 09:29, "rasika " <n.ras...@gmail.com> wrote:
> >
> >
> >
> >
> >
> > > function[a] = checkingtheloops(t)
> > > %for t=[0:1:184]'
> > > n=size(t);
> > > % m=size(a);
> > > % m=n;
> > > a= zeros(n);
> > > for b = 1:size(t)
> > >         if t<=5
> >
> > >             a = t(b)*2;
> >
> > >         elseif(t>5 & t<=15)
> >
> > >                 a = t(b)*3  ;    
> > >         elseif(t>15 & t<=60)
> >
> > >             a =t(b)*4
> >
> > >         end
> > > end
> > > end
> >
> > > "rasika " <n.ras...@gmail.com> wrote in message <j8lloc$5l...@newscl01ah.mathworks.com>...
> > > > x will be passed in workspace. a does give out a column vector as the same size of t.
> > > > I have removed the fprintf and overwriting and index i... still all it gives out is a series of zeros for a..why does it not calculate the values?
> > > > Lothar Schmidt <vapoor...@gmx.net> wrote in message <j8lhvr$49...@news.albasani.net>...
> > > > > Am 31.10.2011 05:22, schrieb rasika:
> > > > > > Hello, I am trying a function where I will need the following kind of
> > > > > > logic of nested loops of for and if else. Can you please let me know
> > > > > > where I am going wrong, itsgiving out all zeros as values of a. it is
> > > > > > not storing values in a and neither going inside the loop
> >
> > > > > > function[a] = checkingtheloops(t)
> > > > > > for t=[0:1:184.7]'
> >
> > > > > overwriting t might be not a good idea
> >
> > > > > > a=zeros(size(t));
> >
> > > > > a will be one single 0
> >
> > > > > > if t<=5
> > > > > > temp = t(i);
> >
> > > > > i not defined so sqrt(-1) will be used as index
> >
> > > > > > a = x*temp;
> >
> > > > > x not defined
> >
> > > > > > fprintf('a');
> >
> > > > > ever read the the documentation of fprintf command?
> >
> > > > > > elseif(t>5 & t<=15)
> > > > > > temp =t(i);
> >
> > > > > same problem as above
> >
> > > > > > a =x*temp;
> >
> > > > > same problem as above
> >
> > > > > > fprintf('a');
> >
> > > > > same problem as above
> >
> > > > > > elseif(t>15 & t<=60)
> > > > > > temp = t(i);
> >
> > > > > same problem as above
> >
> > > > > > a =x*temp;
> >
> > > > > same problem as above
> >
> > > > > > fprintf('a');
> >
> > > > > same problem as above
> >
> > > > > > end
> > > > > > end
> > > > > > end- Zitierten Text ausblenden -
> >
> > > - Zitierten Text anzeigen -
> >
> > You always compare the _complete_ array  t with numbers 5, 15 and 60,
> > but I guess you want to do it _componentwise_:
> >
> > for b = 1:size(t)
> >          if t(b)<=5
> >              a = t(b)*2;
> >          elseif(t(b)>5 & t(b)<=15)
> >              a = t(b)*3  ;
> >          elseif(t(b)>15 & t(b)<=60)
> >              a =t(b)*4;
> >         end
> > end
> >
> > Best wishes
> > Torsten.- Zitierten Text ausblenden -
> >
> > - Zitierten Text anzeigen -
>
> I think even this way:
>
> for b = 1:size(t)
> if t(b)<=5
> a(b) = t(b)*2;
> elseif(t(b)>5 & t(b)<=15)
> a(b) = t(b)*3 ;
> elseif(t(b)>15 & t(b)<=60)
> a(b) =t(b)*4;
> end
> end
>
> Best wishes
> Torsten.

Subject: nested loop trouble

From: rasika

Date: 1 Nov, 2011 02:39:29

Message: 11 of 15

Thank you Steven, That is a very good suggestion, it will make my code more precise, the only trouble is the input matrices would be of varying size since ot would be directly taken from an experimental data, in that case do I have to change the code manually everytime?



"Steven_Lord" <slord@mathworks.com> wrote in message <j8m823$9h$1@newscl01ah.mathworks.com>...
>
>
> "rasika " <n.rasika@gmail.com> wrote in message
> news:j8lloc$5lt$1@newscl01ah.mathworks.com...
> > x will be passed in workspace. a does give out a column vector as the same
> > size of t.
> > I have removed the fprintf and overwriting and index i... still all it
> > gives out is a series of zeros for a..why does it not calculate the
> > values?
>
> When you use:
>
> if vectorCondition
> % do something
> end
>
> the condition will be satisfied, and the code for which "% do something" in
> my example is a placeholder, will be executed only if vectorCondition is
> nonempty and ALL of the elements of vectorCondition are true.
>
> http://www.mathworks.com/help/techdoc/ref/if.html
>
> "An evaluated expression is true when the result is nonempty and contains
> all nonzero elements (logical or real numeric). Otherwise, the expression is
> false."
>
> Since not ALL of the elements of (t<=5) are true, your first IF condition is
> not satisfied. Neither are any of the conditions on your ELSEIF blocks.
>
> I recommend using logical indexing instead.
>
> http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: nested loop trouble

From: rasika

Date: 1 Nov, 2011 02:47:27

Message: 12 of 15

Hey, it worked, but for values greater than 60 it is returning zero. i kind of wanted it to go in cycles.Should i use a temp or dummy variable inside so it will do it.. what I wanted to achieve is 3 set points of numeric values like 5,15, and 60 and in a huge data set i will have multiple cycles of 60, does it make any sense?

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <9b85457d-6f51-453e-9d7d-cffe9d925a06@n38g2000yqm.googlegroups.com>...
> On 31 Okt., 09:29, "rasika " <n.ras...@gmail.com> wrote:
> > function[a] = checkingtheloops(t)
> > %for t=[0:1:184]'
> > n=size(t);
> > % m=size(a);
> > % m=n;
> > a= zeros(n);
> > for b = 1:size(t)
> >         if t<=5
> >
> >             a = t(b)*2;
> >
> >         elseif(t>5 & t<=15)
> >
> >                 a = t(b)*3  ;    
> >         elseif(t>15 & t<=60)
> >
> >             a =t(b)*4
> >
> >         end
> > end
> > end
> >
> >
> >
> > "rasika " <n.ras...@gmail.com> wrote in message <j8lloc$5l...@newscl01ah.mathworks.com>...
> > > x will be passed in workspace. a does give out a column vector as the same size of t.
> > > I have removed the fprintf and overwriting and index i... still all it gives out is a series of zeros for a..why does it not calculate the values?
> > > Lothar Schmidt <vapoor...@gmx.net> wrote in message <j8lhvr$49...@news.albasani.net>...
> > > > Am 31.10.2011 05:22, schrieb rasika:
> > > > > Hello, I am trying a function where I will need the following kind of
> > > > > logic of nested loops of for and if else. Can you please let me know
> > > > > where I am going wrong, itsgiving out all zeros as values of a. it is
> > > > > not storing values in a and neither going inside the loop
> >
> > > > > function[a] = checkingtheloops(t)
> > > > > for t=[0:1:184.7]'
> >
> > > > overwriting t might be not a good idea
> >
> > > > > a=zeros(size(t));
> >
> > > > a will be one single 0
> >
> > > > > if t<=5
> > > > > temp = t(i);
> >
> > > > i not defined so sqrt(-1) will be used as index
> >
> > > > > a = x*temp;
> >
> > > > x not defined
> >
> > > > > fprintf('a');
> >
> > > > ever read the the documentation of fprintf command?
> >
> > > > > elseif(t>5 & t<=15)
> > > > > temp =t(i);
> >
> > > > same problem as above
> >
> > > > > a =x*temp;
> >
> > > > same problem as above
> >
> > > > > fprintf('a');
> >
> > > > same problem as above
> >
> > > > > elseif(t>15 & t<=60)
> > > > > temp = t(i);
> >
> > > > same problem as above
> >
> > > > > a =x*temp;
> >
> > > > same problem as above
> >
> > > > > fprintf('a');
> >
> > > > same problem as above
> >
> > > > > end
> > > > > end
> > > > > end- Zitierten Text ausblenden -
> >
> > - Zitierten Text anzeigen -
>
> You always compare the _complete_ array t with numbers 5, 15 and 60,
> but I guess you want to do it _componentwise_:
>
> for b = 1:size(t)
> if t(b)<=5
> a = t(b)*2;
> elseif(t(b)>5 & t(b)<=15)
> a = t(b)*3 ;
> elseif(t(b)>15 & t(b)<=60)
> a =t(b)*4;
> end
> end
>
> Best wishes
> Torsten.

Subject: nested loop trouble

From: Steven_Lord

Date: 1 Nov, 2011 14:15:54

Message: 13 of 15



"rasika " <n.rasika@gmail.com> wrote in message
news:j8nm51$3sh$1@newscl01ah.mathworks.com...
> Thank you Steven, That is a very good suggestion, it will make my code
> more precise, the only trouble is the input matrices would be of varying
> size since ot would be directly taken from an experimental data, in that
> case do I have to change the code manually everytime?

So don't fix the sizes of your variables; make those sizes depend on the
size of the input.

function y = oneOverX(x)
y = Inf(size(x));
nonzeroX = (x ~= 0);
y(nonzeroX) = 1./x(nonzeroX);

Because y is preallocated to be the same size as x, I can pass any sized x
into this function that I want.

1-by-10? Sure.
100-by-200? Yes.
1-by-2-by-3-by-4-by-5-by-6-by-7-by-8-by-9-by-10? If I have enough memory, it
will work.
0-by-0? Absolutely.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: nested loop trouble

From: rasika

Date: 2 Nov, 2011 03:56:28

Message: 14 of 15

Also I wanted to know is
but for values greater than 60 it is returning zero. i kind of wanted it to go in cycles.Should i use a temp or dummy variable inside so it will do it.. what I wanted to achieve is 3 set points of numeric values like 5,15, and 60 and in a huge data set i will have multiple cycles of 60, does it make any sense?


"Steven_Lord" <slord@mathworks.com> wrote in message <j8ouuq$6e9$1@newscl01ah.mathworks.com>...
>
>
> "rasika " <n.rasika@gmail.com> wrote in message
> news:j8nm51$3sh$1@newscl01ah.mathworks.com...
> > Thank you Steven, That is a very good suggestion, it will make my code
> > more precise, the only trouble is the input matrices would be of varying
> > size since ot would be directly taken from an experimental data, in that
> > case do I have to change the code manually everytime?
>
> So don't fix the sizes of your variables; make those sizes depend on the
> size of the input.
>
> function y = oneOverX(x)
> y = Inf(size(x));
> nonzeroX = (x ~= 0);
> y(nonzeroX) = 1./x(nonzeroX);
>
> Because y is preallocated to be the same size as x, I can pass any sized x
> into this function that I want.
>
> 1-by-10? Sure.
> 100-by-200? Yes.
> 1-by-2-by-3-by-4-by-5-by-6-by-7-by-8-by-9-by-10? If I have enough memory, it
> will work.
> 0-by-0? Absolutely.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: nested loop trouble

From: Steven_Lord

Date: 2 Nov, 2011 13:48:46

Message: 15 of 15



"rasika " <n.rasika@gmail.com> wrote in message
news:j8qf1c$e54$1@newscl01ah.mathworks.com...
> Also I wanted to know is
> but for values greater than 60 it is returning zero. i kind of wanted it
> to go in cycles.Should i use a temp or dummy variable inside so it will do
> it.. what I wanted to achieve is 3 set points of numeric values like 5,15,
> and 60 and in a huge data set i will have multiple cycles of 60, does it
> make any sense?

Take a look at the MOD or REM functions.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for 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