Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:51:38+00:00 2026-06-18T12:51:38+00:00

I am stuck at a problem in matlab. How do I select a number

  • 0

I am stuck at a problem in matlab. How do I select a number ‘i’ with a probability ‘i’ in a matrix.
I want to do this in a matrix, where I select a number ‘i’ from each row, with probability ‘i’.

Any help is appreciated

a sample matrix( dont need to select zeros):

    w1= .47;
    w2= .023;
    m1= .06;
    m2= .037;
    x=rand(1,m1)<=m1;
    tolerance= 0.01;

    Transition=[m1 w1 0  w1 0  0   0;
        0  m1 w1 0  0  0   0;
        0  0  m1 w1 0  0   0;
        0  0  0  m2 w2 0   0;
        0  0  0  0  m2 w1  w1;
        0  0  0  0  0  m1  w1
        0  0  0  0  0  0   m1];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:51:40+00:00Added an answer on June 18, 2026 at 12:51 pm

    First, we could calculate a matrix of cumulative probabilities Tc in preparation for a strategy that finds the first column exceeding a random value between 0 and 1.

    Tc = zeros(size(T));
    Tc(:, 1) = T(:, 1);
    for k = 2 : size(T, 2)
      Tc(:, k) = Tc(:, k - 1) + T(:, k);
    end
    

    To now draw numbers from each row, we first draw p = rand(size(T, 2), 1) and then find the column for which p falls into a bucket of cumulative probability:

    for k = 1 : size(T, 1)
      col = find(T(k, :) > p(k), 1, 'first');
      if isempty(col)
        fprintf('nothing drawn for row %d\n', k);
      else
        fprintf('row %d, col %d, p = %f\n', k, col, T(k, col));
      end
    end
    

    This works because order does not matter. For example, for some p = 0.2, some arbitrary cumulative distribution could increase from 0.65 to 0.85. The probability for rand to yield a value in this interval is indeed 0.2. If this is the last non-zero entry of T, rand will return a value with 0.15 probability for which no number is drawn. In another example, if all entries in T are zero, the cumulative distribution will never exceed what can be drawn by rand. For a last example, if there are two entries at 0.5, each, the cumulative distribution will exceed what’s drawn by rand half and half at either column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with a problem and hope stackoverfolw community can help me. I want
I am stuck in this problem . I want to call the method of
I stuck to this problem. I want to calculate body BMI. My HTML -
I'm stuck with a problem and ask for your help. It's about navigation within
I got stuck in this problem for an hour. I am thinking this is
I got stuck with this problem. I wrap two tables inside of a h:form.
I am stuck with this problem for 3 days with no avail to solve
I know this is silly but i am stuck this problem.i have try this
I'm stuck on this problem. I'm able to load view for new/edit but create/update
I am working in MATLAB and I'm stuck on a very simple problem: I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.