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

2 vues (au cours des 30 derniers jours)
Elias Seppä
Elias Seppä le 19 Nov 2022
Commenté : John D'Errico le 19 Nov 2022
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 commentaires
Elias Seppä
Elias Seppä le 19 Nov 2022
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.
John D'Errico
John D'Errico le 19 Nov 2022
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!

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 19 Nov 2022
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 commentaires
Elias Seppä
Elias Seppä le 19 Nov 2022
Unfortunately, this still didn't work :(
Cris LaPierre
Cris LaPierre le 19 Nov 2022
Modifié(e) : Cris LaPierre le 19 Nov 2022
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

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by