Generating random variable of certain std and mean (Simple Task)

Hi,
Below is a school task. Somehow the value D and the Q are incorrect according to the automatic test software that is checking if my code is correct.
Does anyone know, what I'm doing wrong, this should be pretty simple task.
So the task is:
1) Create artificial data samples (, ) from two normal distributions with mean
values (, ) and standard deviations (, ) (use Matlab's function
randn to generate the samples)
2) From these samples calculate the sample means , and their difference .
Calculate also the sample variances , and their quotient .
My code:
y = 4.2.*randn(1, 20) + 7.2 %First data sample.
y2 = 6.1.*randn(1, 18) + 8.5 %Second data sample.
m1 = mean(y) %
m2 = mean(y2) %
D = m1 - m2 % Difference: .
var1 = (1/(20-1)).*sum((y - m1).^2);
var2 = (1/(20-1)).*sum((y2 - m2).^2);
Q = var1 / var2

12 Comments

"Somehow the value D and the Q are incorrect "
You do not appear to be calculating Q
Looks fine to me.
Maybe you are asked to fix the seed of the random number generator for randn ?
Otherwise I think it's difficult for an automatic software checker to decide whether your code is correct because the results will differ depending on what seed you chose.
Hi Cris,
Now it should be there, Q = var1 / var2.
Calculating the Q might be wrong, but at least the D should be correct I think.
What is the full error message you are getting from the grader? If this is a problem in MATLAB Grader, share all the feedback (especially the red text at the top).
In MATLAB Grader, the reference and learner workspaces share the same random number generator seed, so would both generate the same random numbers (assuming the samples are created in the same order in both scripts)
Here's a JPG file where you can see the error messages from the grader for D and Q.
This is a problem in MATLAB Grader. Please share a screenshot of the full page so that we can see the code you have written and submitted, along with the corresponding assessment test results.
Here's screenshots of the Exercise and the script that i have inserted into MATLAB Grader.
As you can see, there is also a part c) in this exercise but I have not included that code in the matlab grader as of now, because I'm first trying to get this D and Q correct.
Looking at the tests checked in grader, it looks as if you were supposed to perform this 10000 times, getting 10000 results in D, because the length of D is supposed to be 10000. If that is not true, then the tests shown will fail.
I think you need to show us the complete assignment text for this problem.
I see you are no longer getting an error that Q does not exist. Just that t is incorrect. As John D'Errico said, this is because the test is grading the final case - 10000 samples.
So I modified my code.
Now, it calculates the Difference (D), 10 000 times.
And also the Q, 10 000 times.
Now, the matlab grader gives me the correct answer for Q but D it still incorrect.
Oh, I had calculated the mean wrong.
Now, it is all correct.
Thank you all very much for the help, I wouldn't have known that the Matlab Grader is expecting me to calculate this 10 000 times without the help.
Good. In the end, we figured it out, as a group effort.
In hindsight, as long as you can check the tests that are being used, you could have seen that, but it may not have been obvious what was happening even so. My guess is the problem statement was poor. which does happen. Onward and upwards!

Sign in to comment.

 Accepted Answer

My suspicion is that the grader is expecting column vectors but you have created row vectors.
Try this
y = 4.2.*randn(20, 1) + 7.2 %First data sample.
y2 = 6.1.*randn(18, 1) + 8.5 %Second data sample.

2 Comments

Unfortunately, this still didn't work :(
There is some inconsistency between the problem statement you have shared and the pretest code. You say you are supposed to create samples with n=20 and 18, but the pretest code is checking that D has 10000 elements. This suggests to me you are supposed to create 10000 sample data sets.
y = 4.2.*randn(20, 10000) + 7.2; %First data sample.
y2 = 6.1.*randn(18, 10000) + 8.5; %Second data sample.
m1 = mean(y); %
m2 = mean(y2); %
D = m1 - m2; % Difference:
length(D)
ans = 10000

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!