필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Creating a matrix based on information in a vector and another matrix

조회 수: 2 (최근 30일)
Ulrik William Nash
Ulrik William Nash 2017년 7월 5일
마감: MATLAB Answer Bot 2021년 8월 20일
Suppose I have a vector of success probabilities, for example:
p = [0.5,0.25,0.75]
I also have a matrix of outcomes, o, which has the same number of columns as p, containing "0's" (failure) and "1's" (success):
o = [1,0,1
0,0,0]
What I wish to obtain is the following matrix:
m = [p(1,1), 1-p(1,2), p(1,3)
1-p(1,1), 1-p(1,2), 1-p(1,3)]
So, to clarify, 1's in o result in a value equal to the corresponding value in p, while 0's in o lead to 1 minus the corresponding value in p.
What is the most efficient way to obtain m?
  댓글 수: 2
alice
alice 2017년 7월 5일
편집: alice 2017년 7월 5일
If p is a row vector, what do you mean by p(2,3)?
Do you mean:
m = [p(1), 1-p(2), p(3)
1-p(1), 1-p(2), 1-p(3)]
?
Ulrik William Nash
Ulrik William Nash 2017년 7월 5일
Oh, my mistake. Yes. That is, of course, what I meant. Sorry about this.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 7월 5일
편집: Andrei Bobrov 2017년 7월 5일
For MARLAB >= R2016b
out = ~o - p;
out(o>0) = -out(o>0);
MATLAB <= R2016a
out = bsxfun(@minus,~o,p);
out = out.*(1-o*2);
  댓글 수: 4
Ulrik William Nash
Ulrik William Nash 2017년 7월 5일
편집: Ulrik William Nash 2017년 7월 5일
Thank you, Andrei. The following is just what you wrote, but in line with the wording of my question:
m = bsxfun(@minus,~o,p).*(1 - o*2)
I don't have the latest version of MATLAB.
Andrei Bobrov
Andrei Bobrov 2017년 7월 5일
편집: Andrei Bobrov 2017년 7월 5일
As you please. Accept this answer or.. ?

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by