Question

Generate N binary random variables Xi, i E {1,2,.., N] where X 1 or -1 with equal probability in Matlab using rand or randn.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given that

we have to write the MATLAB CODE:

MATLAB CODE:

function adj = random_graph(n,p,E,distribution,fun,degrees)

adj=zeros(n); % initialize adjacency matrix

switch nargin
case 1 % just the number of nodes, n
p = 0.5; % default probability of attachment
for i=1:n
for j=i+1:n
if rand<=p
adj(i,j)=1; adj(j,i)=1;
end
end
end

case 2 % the number of nodes and the probability of attachment, n, p
for i=1:n
for j=i+1:n
if rand<=p
adj(i,j)=1; adj(j,i)=1;
end
end
end

case 3 % fixed number of nodes and edges, n, E
while numedges(adj) < E
i=ceil(rand*n); j=ceil(rand*n);
if not(i==j) % do not allow self-loops
adj(i,j)=adj(i,j)+1; adj(j,i)=adj(i,j);
end
end

otherwise % pick from a distribution; generate n random numbers from a distribution
Nseq=1; % ensure the while loops start
switch distribution
case 'uniform'
while mod(sum(Nseq),2)==1 % make sure # stubs is even
Nseq = ceil((n-1)*rand(1,n));
end
case 'normal'
while mod(sum(Nseq),2)==1 % make sure # stubs is even
Nseq = ceil((n-1)/10*randn(1,n)+(n-1)/2);
end
case 'binomial'
p=0.5; % default parameter for binomial distribution
while mod(sum(Nseq),2)==1 % make sure # stubs is even
Nseq = ceil(binornd(n-1,p,1,n));
end
case 'exponential'
while mod(sum(Nseq),2)==1 % make sure # stubs is even
Nseq = ceil(exprnd(n-1,1,n));
end
case 'geometric'
while mod(sum(Nseq),2)==1 % make sure # stubs is even
Nseq = ceil(geornd(p,1,n));
end
case 'custom'
% pick a number from a custom pdf function
% generate a random number x between 1 and N-1
% accept it with probability fun(x)
while mod(sum(Nseq),2)==1 % make sure # stubs is even
Nseq = [];
while length(Nseq)<n
x = ceil(rand*(n-1));
if rand <= fun(x)
Nseq = [Nseq x];
end
end
end
case 'sequence'
Nseq = degrees;
end

% connect stubs at random
nodes_left = [1:n];
for i=1:n
node{i} = [1:Nseq(i)];
end

while numel(nodes_left)>0 % edges < sum(Nseq)/2

randi = ceil(rand*length(nodes_left));
nodei = nodes_left(randi); % pick a random node
randj = ceil(rand*length(node{nodei}));
stubj = node{nodei}(randj); % pick a random stub

randii = ceil(rand*length(nodes_left));
nodeii = nodes_left(randii); % pick another random node
randjj = ceil(rand*length(node{nodeii}));
stubjj = node{nodeii}(randjj); % pick a random stub

% connect two nodes, as longs as stubs different
if not(nodei==nodeii & stubj==stubjj)
% add new links
adj(nodei,nodeii) = adj(nodei,nodeii)+1;
adj(nodeii,nodei) = adj(nodei,nodeii);
% remove connected stubs
node{nodei} = setdiff(node{nodei},stubj);
node{nodeii} = setdiff(node{nodeii},stubjj);
end

% remove empty nodes
nodes_left1 = nodes_left;
for i=1:length(nodes_left)
if length(node{nodes_left(i)})==0
nodes_left1 = setdiff(nodes_left1,nodes_left(i));
end
end
nodes_left = nodes_left1;

end

end

Pp ton,de 37% end cnd

犬 e nd e nd eud

g) cane gome tie end evnd end

end hode Şnode @fdift (noe.Shode i-5stwbj2

Add a comment
Know the answer?
Add Answer to:
Generate N binary random variables Xi, i E {1,2,.., N] where X 1 or -1 with equal probability in ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The probability density function (pdf) of a Gaussian random variable is: where μ s the mean...

    The probability density function (pdf) of a Gaussian random variable is: where μ s the mean of the random va nable, and σ is the standard deviation . (1) Please plot the pdf of a Gaussian random variable (the height of an average person in Miami valley) in Matlab, if we know the mean is 5 feet 9 inches, and the standard deviation is 3 inches (2) Please generate a large number of instances of such a Gaussian random variable...

  • 5000) of uniformly distributed random numbers between 1 Generate a large number (1000 or and 2...

    5000) of uniformly distributed random numbers between 1 Generate a large number (1000 or and 2 (HINT: use the rand command for generating a uniformly distributed random variable between 0 and 1 and then move it!). b Plot the pdf of the distribution. Use the hist command to obtain the number of samples in a random numbers, define binx as a vector with bins on the x-axis (binx = 1:0.01:2). P histx,binx) will provide you the weights pdf. Compare with...

  • matlab Problem 6: The Matlab command 'randn(m,n) produces an m x n matrix of random numbers...

    matlab Problem 6: The Matlab command 'randn(m,n) produces an m x n matrix of random numbers that are a realization of a white random process with some probability density function. Moreover, the Matlab command 'rand(m,n)' produces an mxn matrix of random numbers between 0 and 1 that are a realization of a white random process with some probability density function. a) Use Matlab to do the following steps: 1) Let u-randn (10000,1); and plot u. 2) Use the command 'mean(u)'...

  • Central Limit Theorem: let x1,x2,...,xn be I.I.D. random variables with E(xi)= U Var(xi)= (sigma)^2 defind Z=...

    Central Limit Theorem: let x1,x2,...,xn be I.I.D. random variables with E(xi)= U Var(xi)= (sigma)^2 defind Z= x1+x2+...+xn the distribution of Z converges to a gaussian distribution P(Z<=z)=1-Q((z-Uz)/(sigma)^2) Use MATLAB to prove the central limit theorem. To achieve this, you will need to generate N random variables (I.I.D. with the distribution of your choice) and show that the distribution of the sum approaches a Guassian distribution. Plot the distribution and matlab code. Hint: you may find the hist() function helpful

  • 2. Generate a large number of Gaussian distributed random numbers with mean 0 and variance 1....

    2. Generate a large number of Gaussian distributed random numbers with mean 0 and variance 1. (HINT: use the randn command) a) Provide a scatter plot of the random numbers b) Plot the pdf of the distribution (similar to 1) and compare with theoretical pdf given in the class handout. c) If I want to generate a Gaussian distributed random numbers with mean 2 and variance 9 from the previously generated set of random numbers, how would I do it?...

  • (a) If var[X o2 for each Xi (i = 1,... ,n), find the variance of X = ( Xi)/n. (b) Let the continuous random variable Y...

    (a) If var[X o2 for each Xi (i = 1,... ,n), find the variance of X = ( Xi)/n. (b) Let the continuous random variable Y have the moment generating function My (t) i. Show that the moment generating function of Z = aY b is e*My(at) for non-zero constants a and b ii. Use the result to write down the moment generating function of W 1- 2X if X Gamma(a, B) (a) If var[X o2 for each Xi (i...

  • L.9) Central Limit Theorem Central Limit Theorem Version 1 says Go with independent random variables (Xi,...

    L.9) Central Limit Theorem Central Limit Theorem Version 1 says Go with independent random variables (Xi, X2, X3, ..., Xs, ...] all with the same cumulative distribution function so that μ-Expect[X] = Expect[X] and σ. varpKJ-Var[X] for all i and j Put As n gets large, the cumulative distribution function of S[n] is well approximated by the Normal[0, 1] cumulative distribution function. Another version of the Central Limit Theorem used often in statistics says Go with independent random variables (Xi....

  • 1. The random variables Xi, X2,.. are independent and identically distributed (iid), each with pdf f given in Assignment 4, Question 1. Let Sn- Xi+.+X Using the Central Limit Theorem and the graph of...

    1. The random variables Xi, X2,.. are independent and identically distributed (iid), each with pdf f given in Assignment 4, Question 1. Let Sn- Xi+.+X Using the Central Limit Theorem and the graph of the standard normal distribution in Figure 1, approximate the probability P(S100 >600). Express your answer in the format x.x-10-x. Verify your answer by simulating 10,000 outcomes of Si00 and counting how many of them are > 600. Show the code 1.00 0.95 0.90 0.85 1.2 1.4...

  • Let Xi , i = 1, · · · , n be a random sample from...

    Let Xi , i = 1, · · · , n be a random sample from Poisson(θ) with pdf f(x|θ) = e −θ θ x x! , x = 0, 1, 2, · · · . (a) Find the posterior distribution for θ when the prior is an exponential distribution with mean 1; (b) Find the Bayesian estimator under the square loss function. (c) Find a 95% credible interval for the parameter θ for the sample x1 = 2, x2...

  • please I need help with excel or matlab part. part 3 Lab 1 BASIC DATA PROCESSING...

    please I need help with excel or matlab part. part 3 Lab 1 BASIC DATA PROCESSING PRE-LAB ASSIGNMENT 1. Read the lab manual carefully so you know what you have to do when you walk into the lab. 2. In a lab, the resistance of a resistor was measured using 50 samples giving the following values: 119.95 (6), 121.32 (5), 119.57 (7), 117.43(1), 120.76 (15), 120.67 (1), 119.78 (8), 121.43(3), 121.82(1), and 118.47 (3) 2 Estimate the average value of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT