Preliminaries for writing simulations: Creating arrays with a loop Activity

Hello, I am working through this MAT LAB project and cannot get rid of one error. Please advise me. Thanks!
Here is my code:
%This is the uneditable portion of the exercise. Note that I cannot change this code block at all:
rng("default");
function xs = my_sample(p)
XDist = makedist("Binomial", N=1, p=p);
LenDist = makedist("NegativeBinomial", r=1, p=0.2);
NumXs = 1 + random(LenDist);
xs = random(XDist, [NumXs, 1]);
end
%This is the portion of the exercise that I am to answer with:
% Fill in here
p = 0:0.2:1;
N = length(p);
ManyRandomVectors = cell(N, 1);
for j = 1:N
ManyRandomVectors{j} = my_sample(p(j));
end
%There is only one error in this code output for the MATLAB grader.
% error: Variable ManyRandomVectors has an incorrect value.
Here is the prompt:
The template code below defines a function my_sample that produces an array of vectors of random numbers of a random length. It takes a parameter p and produces a 1 with probability p and 0 with probability . The length comes from a geometric distribution, which has to be represented as a special case of the negative binomial distribution in MATLAB.
Your goal is to produce a cell array named ManyRandomVectors which consists of the results of calling my_sample with p drawn from 0:0.2:1 using a for loop.
Here's a suggestion for how to implement this.
  • Define p = 0:0.2:1
  • Define N = the length of p.
  • Initialize a cell array named ManyRandomVectors so that it is a column with as many cells as the length of p, and each cell contains an empty array. You can use the cell function to do this.
  • Set up a for loop where a variable j ranges from 1 to N.
  • Within the loop, create a random vector by calling my_sample on the j'th element of p, and store it into the j'th cell of ManyRandomVectors. Remember that cell arrays have special notation for storing something into a cell.

3 Comments

rng("default");
function xs = my_sample(p)
XDist = makedist("Binomial", N=1, p=p);
LenDist = makedist("NegativeBinomial", r=1, p=0.2);
NumXs = 1 + random(LenDist);
xs = random(XDist, [NumXs, 1]);
end
% Fill in here
p = 0:0.2:1;
N = numel(p);
V=cell(N, 1);
for j = 1:N
V{j} = my_sample(p(j));
end
V
V = 6×1 cell array
{[ 0]} {10×1 double} { 4×1 double} { 2×1 double} {15×1 double} { 6×1 double}
rng("default");
W=cell(N, 1);
for j = 1:N
W{j} = my_sample(p(j));
end
W
W = 6×1 cell array
{[ 0]} {10×1 double} { 4×1 double} { 2×1 double} {15×1 double} { 6×1 double}
for j = 1:N
all(W{j}==V{j})
end
ans = logical
1
ans = logical
1
ans = logical
1
ans = logical
1
ans = logical
1
ans = logical
1
X=cell(N, 1);
for j = 1:N
X{j} = my_sample(p(j));
end
X
X = 6×1 cell array
{8×1 double} {9×1 double} {2×1 double} {3×1 double} {[ 1]} {[ 1]}
for j = 1:N
all(X{j}==V{j})
end
ans = logical
1
Arrays have incompatible sizes for this operation.
I see nothing wrong with the code you've written -- I wonder if it's possible you ran it multiple times and the preset RNG state wasn't the same as the grader expected and so something like my third case occurred? I've never tried one of those so not exactly sure how it prevents such an issue or how to solve it from the user end if that were the issue...but about the only thing I can see unless the answer is just not consistent with the problem code.
My professor checked his rubric. He made the mistake. It is better now. Thanks for reviewing this. :)
Thanks for the update...even the best do make mistakes.. <g>

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

V
V
on 9 Feb 2026

Edited:

dpb
on 10 Feb 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!