Matlab if-else within for loop problem

Status
Not open for further replies.

rameshrai

Full Member level 3
Joined
Aug 16, 2010
Messages
158
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
appliedelectronicsengineering.blogspot.com
Activity points
2,272
Hi, can anybody help with the code below. the code never output a(k)=1

Code:
N=4;

for k=1:N;
select(k)=rand;

if (0<select(k)<0.5)
a(k)=1;

elseif (0.5<select(k)<1),
a(k)=2;

end
end

I get the following result-
>> a

a =

0 2 0 0

a(k)=1 never happens, i.e I expected 1 in a output.

thanks
 
Last edited by a moderator:

I don't know MatLib but

select(k)=rand; this looks wrong **broken link removed**


if (0<select(k)<0.5) s/b if (0<select(k)<=0.5)
 
Last edited:

Dear Rameshrai,

I tried make code from you. You should change rand by randn. It will happen a(k)=1
Code:
N=4;

for k=1:N;
select(k)=randn();

    if (0<=select(k)<=0.5)
        a(k)=1;
    elseif(0.5<select(k)<=1)
        a(k)=2;

    end
end
You may be Check again,
Thanks
 
isn't the only difference between rand and randn the distribution? rand is uniform distribution while randn is normal?

shouldn't make any difference in this case
 
You have to be careful with your syntax. This probably doesn't do what you think:
Code:
(0<select(k)<0.5)
because it is actually two comparisons, each returning 0 or 1. Take this simple example:
Code:
(1 < 2 > 1)
This is always 0 because (1<2) = 1. Then, ((1<2)>1) = (1>1) = 0.

Try this instead:
Code:
N=4;

% Preallocate storage
select = zeros(1,N);
a = zeros(1,N);
for k = 1:N;
    select(k) = rand;

    if (0<select(k) && select(k)<0.5)
        a(k) = 1;
    elseif (0.5<select(k) && select(k)<1),
        a(k) = 2;
    end
    
end

disp(['Input: ', num2str(select)]);
disp(['Result: ', num2str(a)]);

Now, as a final step, it is generally good to try to "vectorise" your code in Matlab (eliminate FOR loops). Therefore, we can write:
Code:
N=4;

select = rand(1,N);
a = zeros(1,N);

a(0<select & select<0.5) = 1;
a(0.5<select & select<1) = 2;

disp(['Input: ',num2str(select)]);
disp(['Result: ',num2str(a)]);

Note that here we use a single "&" (which is the bitwise AND operator), whereas before we used a double "&&" (which is the scalar AND operator).
 
Last edited:
Thanks guys,

I am also ensure about the following-
Code:
for k=1:N;
select(k)=rand;

if (0<select(k) && select(k)<0.5)                 % (or (0<=select(k)<=0.5) )  (btw which is preferable)
         x(k) = rand;
         y(k) =  rand;

elseif (0<select(k) && select(k)<0.5)           
          a(k) = rand;
          b(k) = rand;
              if(....)
                    c(k) = rand;
                    d(k) = rand;

what I want to do is to generate random numbers x,y,a,b,c,d all having independent random numbers for every k loop value. Does above code work?

lot of thanks
 
Last edited:

Please describe exactly what you are trying to do. This is not clear:
what I want to do is to generate random numbers x,y,a,b,c,d all having independent random numbers for every k loop value.

If you just want independent random numbers, then you can just use:
Code:
N = 4;
x = randn(1,N);
y = randn(1,N);
a = randn(1,N);
b = randn(1,N);
c = randn(1,N);
d = randn(1,N);

for k = 1:N
    z = a(k) + b(k) - 1.234*c(k);
    % ... etc.
end

However, I think you need something more than this. Please describe exactly what you are trying to do with the 'if' statements.
 

Hi weetabixharry,

The random number in your code is like predefined but I want the random numbers (x,y,a,b..) generated within the if elseif container if the their condition within if,elseif are satisfied but with each loop k each should have different numbers. I think my code above works !! but I wanted to be sure

But I am confused about another matlab rule now. Suppose I have the following-

Code:
clear all;

N=16;

a = zeros(1,N);
b = zeros(1,N);
d = zeros(1,N);
e = zeros(1,N);
g = zeros(1,N);

for k=1:N
    
    a(k)=rand;
        if(a(k)<0.5)
            b(k)=0;
        else
            b(k)=1;
        end
        
    d(k)=rand;
    
        if(0<d(k) && d(k)<0.2)
            e(k)=1;
        elseif(0.2<d(k) && d(k)<0.4)
            e(k)=2;
        elseif(0.4<d(k) && d(k)<0.6)
            e(k)=3;
        elseif(0.6<d(k) && d(k)<0.8)
            e(k)=4;
        elseif(0.8<d(k) && d(k)<1)
            e(k)=5;
        end


        if(b(k)==0) && (e(k)==1)        
            g(k) = 1;
        elseif(b(k)==0 && e(k)==2)   
            g(k) = 2;          
        elseif(b(k)==0 && e(k)==3)     
            g(k) = 3;
        elseif(b(k)==0 && e(k)==4)     
            g(k) = 4;
        elseif(b(k)==1 && e(k)==1)     
            g(k) = 5;
        elseif(b(k)==1 && e(k)==2)    
            g(k) = 6;
        elseif(b(k)==1 && e(k)==3)  
            g(k) = 7;
        elseif(b(k)==1 && e(k)==4)   
            g(k) = 8;          
        elseif(b(k)==(0 || 1) && e(k) == 5)
            g(k) = 9;
        end
    
end
disp(['a: ', num2str(a)]);
disp(['b: ', num2str(b)]);
disp(['d: ', num2str(d)]);
disp(['e: ', num2str(e)]);
disp(['g: ', num2str(g)]);

Here, b(k) are 0s & 1s(binary bits) whereas e(k) are integers,

The problem is I get zeros in g output which I expected not to happen.

I read about logical comparision, bitwise comparision ..... is that because of this I am gettting zeros in g? because b(k) are 0's and 1's and e(k) are integers within the if and elseif conditions.

Thank you
 
Last edited:
Just looking quickly, I can see you have the same problem as before. This line:
Code:
elseif(b(k)==(0 || 1) && e(k) == 5)
would be exactly the same as writing:
Code:
elseif(b(k)==1 && e(k) == 5)
because (0 || 1) = 1. In order to catch the zero case, you can use:
Code:
elseif((b(k)==0 || b(k)==1) && e(k) == 5)

Also, as a separate point, when you are investigating ranges of numbers like in your code, you should really use "<=" (less than or equal) or ">=" (greater than or equal), otherwise you will miss the "equal" case. So, your first if statements could be:

Code:
if(0<d(k) && d(k)<=0.2)
    e(k)=1;
elseif(0.2<d(k) && d(k)<=0.4)
    e(k)=2;
elseif(0.4<d(k) && d(k)<=0.6)
    e(k)=3;
elseif(0.6<d(k) && d(k)<=0.8)
    e(k)=4;
elseif(0.8<d(k) && d(k)<1)
    e(k)=5;
end

Note that we don't need to worry about d(k)==0 or d(k)==1 because the Matlab function rand only returns values on the interval (0,1) (i.e. not uncluding 0 or 1).

The random number in your code is like predefined
My "vectorised" method is actually exactly the same, but will execute much more quickly (especially for larger N) in Matlab. When you are using very small N (like 4 or 16), you can use your method if it helps you to understand. However, after vectorising, your whole code could be replaced with this:
Code:
N=16;

a = rand(1,N);
b = zeros(1,N);
b(a>=0.5) = 1;

d = rand(1,N);
e = ones(1,N);
e(0.2<d & d<=0.4) = 2;
e(0.4<d & d<=0.6) = 3;
e(0.6<d & d<=0.8) = 4;
e(0.8<d & d<1) = 5;

g = e + 4*(b==1);
 
Last edited:
i think, when in for statement is k=1, the if statement is not perform.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…