How is Matlab performing this multiplication?

1 view (last 30 days)
Ok, I have a chunk of code here. I wanted to point out that I'm really interested in how tables in a cell array are being multiplied by Matlab. Because by inspection, it seems to change the order of the elements around in such a way, that the probabilities I was trying to calculate might not make sense. For starters,
butler=3; maid=2; knife=1; % Variable order is arbitary
murderer=1; notmurderer=2; used=1; notused=2; % define states, starting from 1.
% The following definitions of variable are not necessary for computation,
% but are useful for displaying table entries:
variable(butler).name='butler'; variable(butler).domain = {'murderer','not murderer'};
variable(maid).name='maid'; variable(maid).domain ={'murderer','not murderer'};
variable(knife).name='knife'; variable(knife).domain={'used','not used'};
% Three potential since p(butler,maid,knife)=p(knife|butler,maid)p(butler)p(maid).
% potential numbering is arbitary
pot{butler}=array;
pot{butler}.variables=butler;
pot{butler}.table(murderer)=0.6;
pot{butler}.table(notmurderer)=0.4;
pot{maid}=array;
pot{maid}.variables=maid;
pot{maid}.table(murderer)=0.2;
pot{maid}.table(notmurderer)=0.8;
pot{knife}=array;
pot{knife}.variables=[knife,butler,maid]; % define array below using this variable order
pot{knife}.table(used, notmurderer, notmurderer)=0.3;
pot{knife}.table(used, notmurderer, murderer) =0.2;
pot{knife}.table(used, murderer, notmurderer)=0.6;
pot{knife}.table(used, murderer, murderer) =0.1;
pot{knife}.table(notused,:,:)=1-pot{knife}.table(used,:,:); % due to normalisation
jointpot = multpots(pot); % joint distribution
So, if I were to look at pot{knife}.table I would see:
ans(:,:,1) =
0.1000 0.2000
0.9000 0.8000
ans(:,:,2) =
0.6000 0.3000
0.4000 0.7000
Here, the table element (1,1,1), corresponding to the probability of knife being used, given that the Butler isn't the murderer and the Maid is, is 0.2
Now if I multiply this by pot{maid}, this table turns into:
ans(:,:,1) =
0.0200 0.4800
0.1800 0.3200
ans(:,:,2) =
0.0400 0.2400
0.1600 0.5600
Now, Matlab seems to have multiplied the probabilities together correctly, however if again I look at this table and want to see the probability associated with the knife being used, the Butler not being the murder, andt he Maid being the murderer, I get 0.48. But that's not pot{knife}.table(used, notmurderer, murderer) =0.2 multiplied by the probability of the Maid being the murderer (0.2). This would be 0.04. But in this new table, 0.04 corresponds to entry (1,1,2) which is the probability of the knife being used, the Butler being the murderer, the Maid is not the murderer, times the probability of the Maid not being the murderer.
Why this reorganization? I don't understand the mechanics of this - although the good thing is that it looks like Matlab is correctly matching probabilities together, even if they wind up in (to me) an unexpected location int he resulting table. Can anyone suggest how I can figure out what's going so I can anticipate this behavior in future programming exercises?
  1 Comment
Matt J
Matt J on 20 Sep 2013
Now if I multiply this by pot{maid}, this table turns into:
You cannot have multiplied by pot{maid} because pot{maid} is not numeric, but rather a struct with fields "variables" and "table". Show us the actual code you used to perform the multiplication.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!