Solved

find all possible sequence value from a matrix

Posted on 2006-10-30
1
172 Views
Last Modified: 2012-05-05
hi,


 | a     |   b     |     c     |
---------------------------------|
a| 0.5  | 0.25   | 0.25    | =1
---------------------------------|    
b| 0.375| 0.125 | 0.375  | =1
---------------------------------|
c| 0.125| 0.675 | 0.375  | =1
-----------------------------------


I need calculate the probability from a above matrix

for example, if I want to find a sequence of aab, it should calculate like:
a-->a-->b = 0.5 * 0.25 = 0.125

could someone give me some codes to find "all possible paths" for any given sequence? for example for sequence of 3, there is 3^3=27 possible paths (aaa,aab,aac, aba....ccc.etc). I want to find the probability of all 27 paths.
0
Comment
Question by:rmtogether
1 Comment
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17838934
The following code,

  double[,] matrix = new double[,] { { 0.5, 0.25, 0.25 }, { 0.375, 0.125, 0.375 }, { 0.125, 0.675, 0.375 } };
  System.Diagnostics.Debug.Assert(matrix.GetLength(0) == matrix.GetLength(1));

  int sequences = 3;

  for (int i = 0; i < (int)Math.Pow(matrix.GetLength(0), sequences); i++)
  {
    double p = 1;
   
    Console.Write((char)('a' + ((i / (int)Math.Pow(matrix.GetLength(0), 0)) % matrix.GetLength(0))));

    for (int j = 1; j < sequences; j++)
    {
      int a = (i / (int)Math.Pow(matrix.GetLength(0), j - 1)) % matrix.GetLength(0);
      int b = (i / (int)Math.Pow(matrix.GetLength(0), j)) % matrix.GetLength(0);
      p *= matrix[a, b];

      Console.Write("->" + (char)('a' + b));
    }

    Console.WriteLine(" " + p);
  }

  Console.ReadLine();

produces the output ("a->a->b 0.125"),

a->a->a 0.25
b->a->a 0.1875
c->a->a 0.0625
a->b->a 0.09375
b->b->a 0.046875
c->b->a 0.253125
a->c->a 0.03125
b->c->a 0.046875
c->c->a 0.046875
a->a->b 0.125
b->a->b 0.09375
c->a->b 0.03125
a->b->b 0.03125
b->b->b 0.015625
c->b->b 0.084375
a->c->b 0.16875
b->c->b 0.253125
c->c->b 0.253125
a->a->c 0.125
b->a->c 0.09375
c->a->c 0.03125
a->b->c 0.09375
b->b->c 0.046875
c->b->c 0.253125
a->c->c 0.09375
b->c->c 0.140625
c->c->c 0.140625
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String manipulation 15 64
Handle null when using linq in this line 1 26
VB.NET HttpWebRequest 12 55
How to avoid saving tif files a 2nd time using VS2015 and C#? 1 27
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now