rameshrai
Full Member level 3
- Joined
- Aug 16, 2010
- Messages
- 158
- Helped
- 7
- Reputation
- 14
- Reaction score
- 7
- Trophy points
- 1,298
- Activity points
- 2,272
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
>> a
a =
0 2 0 0
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
(0<select(k)<0.5)
(1 < 2 > 1)
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)]);
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)]);
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;
Please read my above post. I answered this in detail.(or (0<=select(k)<=0.5) ) (btw which is preferable)
Please read my above post. I answered this in detail.
Please describe exactly what you are trying to do and I will help.
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.
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
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)]);
elseif(b(k)==(0 || 1) && e(k) == 5)
elseif(b(k)==1 && e(k) == 5)
elseif((b(k)==0 || b(k)==1) && e(k) == 5)
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
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:The random number in your code is like predefined
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);
Why do you think this?i think, when in for statement is k=1, the if statement is not perform.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?