필터 지우기
필터 지우기

Create a matrix of random numbers with different distributions in each entry

조회 수: 2 (최근 30일)
Say I have a matrix and I want to add an error to each of the elements, the error should be normally distributed around the element with variance of 0.1 of the value of the element.
That is how I implemented this:
A = [1 2 3 ; 4 5 6 ; 7 8 9]
sz=size(A);
sigma = 0.1*A;
err = zeros(sz);
for i=1:sz(1)
for j=1:sz(2)
pd = makedist('Normal','sigma',sigma(i,j));
err(i,j)=random(pd);
end
end
Result = A + err;
This is ok for 9 elemens but it is very time consuming when my matrix is 3000X3000
Is there any more efficient way to do so?

채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 2월 23일
편집: Thiago Henrique Gomes Lobato 2020년 2월 23일
Your code does basically this:
A = [1 2 3 ; 4 5 6 ; 7 8 9]
Result = A + randn(size(A))*0.1;
In your question however you seem to mean that the variance should be 0.1 of the element value, if this is the case do this:
A = [1 2 3 ; 4 5 6 ; 7 8 9]
Result = A.*(1+randn(size(A))*0.1)
I'm not sure what you actually want but those two solutions should solve it way faster than a loop
  댓글 수: 4
Pavel Penshin
Pavel Penshin 2020년 2월 23일
doest this mean that effectively my error matrix is satisfying the condition that
err(i,j) = rand(makedist('Normal','sigma',0.1*A(i,j)))
Because I need the errors matrix itself as well.
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020년 2월 23일
Yes. If you need the error matrix you can do it like this:
err = A.*randn(size(A))*0.1
Result = A+err;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by