Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
Ulrik William Nash am 5 Jul. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 KommentareKeine anzeigenKeine ausblenden
alice am 5 Jul. 2017
Bearbeitet: alice am 5 Jul. 2017
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 am 5 Jul. 2017

### Antworten (1)

Andrei Bobrov am 5 Jul. 2017
Bearbeitet: Andrei Bobrov am 5 Jul. 2017
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 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Ulrik William Nash am 5 Jul. 2017
Bearbeitet: Ulrik William Nash am 5 Jul. 2017
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)
Andrei Bobrov am 5 Jul. 2017
Bearbeitet: Andrei Bobrov am 5 Jul. 2017