How to generate bivariate random normally distributed 3d array?

mu= [0 0]
mu = 1×2
0 0
sigma= [1 0.25; .25 1]
sigma = 2×2
1.0000 0.2500 0.2500 1.0000
mvnrnd(mu,sigma,100)
ans = 100×2
0.7082 0.2195 -2.3244 -1.9734 -1.8774 0.1327 -0.2237 1.3953 -0.7603 0.4547 -1.2340 0.0526 0.8824 0.1801 0.1022 0.6385 -1.9730 -1.0128 1.3997 0.4401
I want to generate a 3-by-2-by-100 array which is normally distributed with given mu and sigma. Please help me how can I do this?

5 Commenti

A two-dimensional random variable produces pairs of values as realizations (like the 100 pairs in your code).
Do you really think it's useful to put these pairs in the 2nd dimension of a 3d matrix ?
I don't understand what the first dimension (with size 3 in your 3-by-2-by-100 array) is supposed to represent. As @Torsten points out, you have 100 sets of 2 parameters. Where does the 3 come in?
Maybe the OP want to generate 3 arrays of size ( 100 x 2 ).' :-)
Since the realizations of such a random variable are i.i.d., it is sort of irrelevant what the 1st and third diemsnions mean!
Torsten
Torsten il 31 Mag 2023
Modificato: Torsten il 31 Mag 2023
Yes, but don't you agree that for the k-dimensional case (see below), it's much more handy for access to have the realizations in a (n x k) matrix instead of a whatever matrix ?

Accedi per commentare.

 Risposta accettata

Um, trivial?
You apparently want to generate 300 samples of a bivariate normal. So generate them as a 300x2 array, Then reshape and permute them into the desired 3x2x100 array.
mu= [0 0];
sigma= [1 0.25; .25 1];
X = mvnrnd(mu,sigma,300);
X = reshape(X,[3,100,2]);
X = permute(X,[1 3 2]);
size(X)
ans = 1×3
3 2 100

3 Commenti

Thank you so much. I was trying to generate like this.
Can you explain me the utility of using permute after reshape?
If mu= [0 0 0] and sigma= 3-by-3 then how can I proceed in case of multivariable?
Permute is like transpose. But it applies to arrays of multiple dimensions.
Look at what I did.
mu= [0 0];
sigma= [1 0.25; .25 1];
X = mvnrnd(mu,sigma,300);
What is the initial size of X?
size(X)
ans = 1×2
300 2
I generated 300 samples of bivariate random numbers with the desired distribution. What did reshape do?
X = reshape(X,[3,100,2]);
size(X)
ans = 1×3
3 100 2
So I simply reshaped that into an array of 3x100 instances of the same random numbers. FInally, the call to permute does nothing more than transpose the second and third dimensions.
X = permute(X,[1 3 2]);
size(X)
ans = 1×3
3 2 100
Again, permute is just like transpose. It allows you to transpose any dimensions you wish. It applies to 3-d and higher dimension arrays.
You will do exactly the same thing, IF you wanted to generate trivariate random numbers. But now you will have a 3 instead of a 2 in those places in the reshape and permute.
Thank you so much for the explaination

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by