"Konst " <konstance1@hotmail.com> wrote in message <ilqu4q$5pt$1@ginger.mathworks.com>...
> "someone" wrote in message <ilqpga$lb9$1@ginger.mathworks.com>...
> > "Konst " <konstance1@hotmail.com> wrote in message <ilqnm9$fs0$1@ginger.mathworks.com>...
> > > "someone" wrote in message <ilqhj9$ous$1@ginger.mathworks.com>...
> > > > "Konst " <konstance1@hotmail.com> wrote in message <ilqe2o$d5o$1@ginger.mathworks.com>...
> > > > > I have this code:
> > > > > clear all
> > > > > close all
> > > > > clc
> > > > > %number of pulses N
> > > > > N=100
> > > > > % returns a NbyN matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1)
> > > > > n1=rand(N,1);
> > > > > %some results I need
> > > > > Q=100*log(n1);
> > > > > n2=rand(N,1);
> > > > > W=100*log(n2);
> > > > > %put Ws and Qs in a vector A starting from W1,Q1,...
> > > > > A=zeros(length(W)+length(Q),1);
> > > > > A(1:2:end1)=W;
> > > > > A(2:2:end)=Q;
> > > > > A;
> > > > > %approximation
> > > > > A=round(A);
> > > > > %put ones and zeros in a vector b in proportion with the values in A
> > > > > s = cumsum(A);
> > > > > b(s+1) = (1).^(0:length(s)1);
> > > > > b(end) = [];
> > > > > b = cumsum(b);
> > > > > plot (b)
> > > > > saveas (gcf,'b.jpg')
> > > > >
> > > > > When I run it for the first time It produces the exact results I need. But in the second time it starts putting other values in b except from 0 and 1,e.g. 2 and 3. It does the same when i increase my N value to N=1000.ANy ideas why??
> > > >
> > > > % I'm not sure, but something seems suspicious in:
> > > > b(s+1) = (1).^(0:length(s)1);
> > > >
> > > > % You are asigning a vector to a specific element of b
> > > > % I'm surprized MATLAB doesn't generate an error message.
> > > > % Perhaps you mean something like:
> > > > b(1:s+1) = (1).^(0:length(s)1);
> > > > % or simply
> > > > b = (1).^(0:length(s)1);
> > > I thought of that too! I tried your suggestions but still the same problems...Thank you!
> >
> > % Well, I made the change:
> > b = (1).^(0:length(s)1);
> > % and no matter how many times I run the code,
> > % the plot goes from 1 to 200 on the xaxis and
> > % from 0 to 1 bin the yaxis.
> > % Before the change the xaxis went to 1.8X10^4
> > % and the yaxis went upto 3 in some cases.
> >
> > % What result are you expecting?
>
> What you're suggesting, puts the values 0 and 1 in b alternately but I want in proportion with the values in A. For the odd cells put 0 and for the even 1 like this:
> for A=[2 3 ...]
> b=[0 0 1 1 1...].
> So eventually, the graph it is plotting is also wrong! Maybe there is another way to do this?
% OK then, if I understand, if
A = [1 2 3 4 5]
% you want:
b = [0 1 1 0 0 0 1 1 1 1 0 0 0 0 0]
% So, try this (given A) short example:
b = zeros(1,sum(A));
b(1:A(1)) = zeros(1,A(1));
for ii = 2:length(A)
if mod(ii,2)
b = [b ones(1,A(ii))];
else
b = [b zeros(1,A(ii))];
end
end
% That should get you most of the way there.
