generate correlated random values in two dimension
40 ビュー (過去 30 日間)
古いコメントを表示
Hello
I want to generate a random field of values like this figure, where each element has the same mean and COV values and the random values vary based on a correlation function (e.g. Markovian)..
Could you please give me a suggestion how to generate random values in two dimension which are correlated based on a correlation function?
0 件のコメント
採用された回答
Shubh Dhyani
2023 年 3 月 1 日
Hi Pooneh,
I understand that you want to generate random values in two dimension which are correlated based on a correlation function.
Here are the steps to generate a two-dimensional Gaussian random field with specified mean, covariance, and correlation function in MATLAB:
1. Define the correlation function. For example, the Markovian correlation function can be defined as C(x, y, a) = exp(-sqrt((x - y).^2) / a), where x and y are the spatial coordinates and a is the correlation length.
2. Define the grid of points where you want to generate the random field. For example, you can create a 2D grid of n by n points using the “meshgrid” function with the “linspace” function to define the range of x and y coordinates. You can refer to these links for the same:
3. Calculate the covariance matrix for the grid of points based on the correlation function. For example, you can use a nested for-loop to compute the covariance between each pair of points using the correlation function.
4. Generate a set of uncorrelated Gaussian random variables with zero mean and unit variance using the “randn” function. You can refer to the link for help regarding the function:
5. Use the Cholesky decomposition to transform the uncorrelated variables to a set of correlated variables with the desired covariance matrix. The “chol” function can be used to compute the Cholesky decomposition. Here’s the link you can refer for the same :
6. Transform the correlated variables to the desired random field with the specified mean and standard deviation. For example, you can use the reshape function to convert the correlated variables to a 2D matrix and then apply a linear transformation to set the mean and standard deviation of the random field.
2 件のコメント
Shubh Dhyani
2023 年 3 月 6 日
Hi Pooneh,
There could be other reasons why the code is not giving the correct covariance matrix, especially in the vertical direction. Here is one possible fix:
1. The “chol” function computes the Cholesky decomposition of a matrix, which returns a lower triangular matrix L such that ‘cov = L*L'. In your original code, you did not specify the lower parameter in the chol function, so it defaulted to computing the upper triangular matrix U instead of L. This caused a problem when you later tried to multiply L with the random vector kkk, resulting in an error.
2. You can remove the element-wise multiplication (.*) between L and kkk in the expression Kh=exp((L.*kkk)+(meanelementKH)) , since kkk is a matrix and L is a lower triangular matrix. Instead, you can use matrix multiplication (*) to compute the product L*kkk.
The modified code should look like this,
tetalnx=200; %horizontal correlation length
tetalny=1.5;%vertical correlation length
SDelementKH=0.5016;
meanelementKH=-2.346;
x=linspace(0.25,49.75,100);
y=linspace(0.25,49.75,100);
[X,Y] = meshgrid(x,y); %coordinates of the centre of the elements in the grid
% i=0;
% j=0;
for j=1:100
for i=1:100
% for z=1:100
% for w=1:100
cov(j,i)=(SDelementKH^2)*exp(-sqrt((2*(x(j)-X(j,i))./tetalnx).^2+(2*(y(j)-Y(j,i))./tetalny).^2)); %covariance based on the distance between the points as the centres of the elements in the mesh grid
end
end
L=chol(cov,"lower");
kkk=randn(100); %Generate a set of uncorrelated Gaussian random variables with zero mean and unit variance using the "randn"
Kh=exp((L*kkk)+(meanelementKH));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!