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 9273653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:09:52+00:00 2026-06-18T16:09:52+00:00

So let me start off by saying that I do not have the statistics

  • 0

So let me start off by saying that I do not have the statistics toolbox for Matlab so I am trying to find a way to work around this. In any case, what I am trying to do is to replicate the R sample function. For example, in R

> x = sample(1:5,20,replace=T,prob=c(.1,.1,.1,.1,.6))
> x
 [1] 5 5 5 4 5 2 5 5 1 5 5 5 5 5 5 3 5 1 5 5

so I am sampling the integers 1,2,3,4,5 with replacement. But furthermore, I am sampling each integer with a certain proportion, i.e., the integer 5 should be sampled about 60% of the time.

So my question that I would like to find a solution to is how to achieve this in Matlab?

  • 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-18T16:09:53+00:00Added an answer on June 18, 2026 at 4:09 pm

    Here’s how you can perform weighted sampling with replacement (something Matlab’s randsample doesn’t support, btw);

    function r = sample(pop,n,weights)
    %# each weight creates a "bin" of defined size. If the value of a random number
    %# falls into the bin, we pick the value
    
    %# turn weights into a normed cumulative sum
    csWeights = cumsum(weights(:))/sum(weights);
    csWeights = [0;csWeights(1:end-1)];
    
    %# for each value: pick a random number, check against weights
    idx = sum(bsxfun(@ge,rand(1,n),csWeights),1);
    
    r = pop(idx);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me start off by saying that I am not an expert in C.
Okay let me start off by saying I have been looking around a lot
Ok, let me start off by saying that I'm don't have the slightest clue
First off, let me start by saying that I am totally new to working
Let me start out by saying that I'm not a C developer and I
First let me start off by saying I do not believe I am leaking,
Let me start off by saying that I am very new to WPF and
Let me start off by saying that I am fairly new to php. I
Preface Let me start off by saying that I'm a relatively new programmer and
So let me start off by saying I'm not a designer by any means,

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.