필터 지우기
필터 지우기

matrix multiplication with probability

조회 수: 2 (최근 30일)
Santhosh Chandrasekar
Santhosh Chandrasekar 2018년 2월 11일
댓글: Santhosh Chandrasekar 2018년 2월 14일
Hi, I want to assign a probability of multiplication for each value in a 3X3 matrix and multiply it by a random number. Can anyone please help me with this.
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 2월 11일
An example would help our understanding.
Santhosh Chandrasekar
Santhosh Chandrasekar 2018년 2월 11일
편집: Santhosh Chandrasekar 2018년 2월 12일
Thank you Jan for your answer, I sincerely appreciate it. but my requirement is for example
A = [1 0.5 1; 0.6 0.3 0.4; 0.4 0.5 1];
K= 1:1:10
a = rand;
C= [a 0 0]; here i want to assign probabilities to the 3 positions of 'C' where 'a' can be assigned i.e C = [10% 20% 30%] so during every iteration, a random 'a' is generated and it is placed in one of the position of 'C' based on the assigned probability and 'C' will get multiplied with 'A'

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

채택된 답변

Roger Stafford
Roger Stafford 2018년 2월 12일
편집: Roger Stafford 2018년 2월 12일
If p is a matrix the same size as A with the probability values you describe, do this:
SA = size(A);
a = rand(SA);
A = A + (A.*a-A).*(rand(SA)<=p);
At each element of A it is multiplied by the random number in ’a’ with probability in the corresponding element in p. Otherwise the element is unchanged.
It is possible you meant 'a' to be a single random scalar value rather than a matrix of them. If so, change the code to:
a = rand;
A = A + ((a-1)*A).*(rand(size(A))<=p);
  댓글 수: 3
Roger Stafford
Roger Stafford 2018년 2월 12일
I will help with your comment request by showing you how to produce an index with the requested probabilities. You can then use that index in dealing appropriately with your matrix A.
P = [.1;.2;.3];
CP = cumsum(P);
r = rand;
ix = sum(CP<=r)+1;
Given the values in P, the index ‘ix’ will assume the values 1, 2, 3, or 4 with respective probabilities .1, .2, .3, and 1-(.1+.2+.3)=.4 . If you wish to do something in the case ix=4, you can write
if ix<=3
% Handle the ix = 1,2,3 cases
else
% Handle the ix = 4 case
end
Otherwise, just omit the ‘else’ part.
Santhosh Chandrasekar
Santhosh Chandrasekar 2018년 2월 14일
I got it. thank you very much!.

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

추가 답변 (1개)

Jan
Jan 2018년 2월 11일
Maybe:
A = [1 0.5 1; 0.6 0.3 0.4; 0.4 0.5 1];
for k = 1:10
a = rand;
B = A * a;
...
end
  댓글 수: 1
Santhosh Chandrasekar
Santhosh Chandrasekar 2018년 2월 12일
편집: Santhosh Chandrasekar 2018년 2월 12일
Thank you Jan for your answer, I sincerely appreciate it. but my requirement is for example
A = [1 0.5 1; 0.6 0.3 0.4; 0.4 0.5 1];
K= 1:1:10
a = rand;
C= [a 0 0]; here i want to assign probabilities to the 3 positions of 'C' where 'a' can be assigned i.e C = [10% 20% 30%] so during every iteration, a random 'a' is generated and it is placed in one of the position of 'C' based on the assigned probability and 'C' will get multiplied with 'A'

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

카테고리

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