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:
wrong code?

Subject: wrong code?

From: Konst

Date: 16 Mar, 2011 13:31:04

Message: 1 of 7

I have this code:
clear all
close all
clc
%number of pulses N
N=100
% returns a N-by-N 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:end-1)=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??

Subject: wrong code?

From: someone

Date: 16 Mar, 2011 14:31:05

Message: 2 of 7

"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 N-by-N 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:end-1)=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);

Subject: wrong code?

From: Konst

Date: 16 Mar, 2011 16:15:05

Message: 3 of 7

"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 N-by-N 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:end-1)=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!

Subject: wrong code?

From: someone

Date: 16 Mar, 2011 16:46:02

Message: 4 of 7

"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 N-by-N 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:end-1)=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 x-axis and
% from 0 to 1 bin the y-axis.
% Before the change the x-axis went to 1.8X10^4
% and the y-axis went upto 3 in some cases.

% What result are you expecting?

Subject: wrong code?

From: Konst

Date: 16 Mar, 2011 18:05:14

Message: 5 of 7

"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 N-by-N 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:end-1)=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 x-axis and
> % from 0 to 1 bin the y-axis.
> % Before the change the x-axis went to 1.8X10^4
> % and the y-axis 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?

Subject: wrong code?

From: someone

Date: 16 Mar, 2011 19:12:05

Message: 6 of 7

"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 N-by-N 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:end-1)=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 x-axis and
> > % from 0 to 1 bin the y-axis.
> > % Before the change the x-axis went to 1.8X10^4
> > % and the y-axis 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.

Subject: wrong code?

From: Konst

Date: 16 Mar, 2011 19:56:05

Message: 7 of 7

It seems to work ok!!Thank you once again...

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