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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:12:59+00:00 2026-05-29T09:12:59+00:00

How can I take a sample of n random points from a matrix populated

  • 0

How can I take a sample of n random points from a matrix populated with 1’s and 0’s ?

a=rep(0:1,5)
b=rep(0,10)
c=rep(1,10)
dataset=matrix(cbind(a,b,c),nrow=10,ncol=3)

dataset
      [,1] [,2] [,3]
 [1,]    0    0    1
 [2,]    1    0    1
 [3,]    0    0    1
 [4,]    1    0    1
 [5,]    0    0    1
 [6,]    1    0    1
 [7,]    0    0    1
 [8,]    1    0    1
 [9,]    0    0    1
[10,]    1    0    1

I want to be sure that the positions(row,col) from were I take the N samples are random.

I know sample {base} but it doesn’t seem to allow me to do that, other methods I know are spatial methods that will force me to add x,y and change it to a spatial object and again back to a normal matrix.

More information

By random I mean also spread inside the “matrix space”, e.g. if I make a sampling of 4 points I don’t want to have as a result 4 neighboring points, I want them spread in the “matrix space”.

Knowing the position(row,col) in the matrix where I took out the random points would also be important.

  • 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-05-29T09:13:00+00:00Added an answer on May 29, 2026 at 9:13 am

    There is a very easy way to sample a matrix that works if you understand that R represents a matrix internally as a vector.

    This means you can use sample directly on your matrix. For example, let’s assume you want to sample 10 points with replacement:

    n <- 10
    replace=TRUE
    

    Now just use sample on your matrix:

    set.seed(1)
    sample(dataset, n, replace=replace)
     [1] 1 0 0 1 0 1 1 0 0 1
    

    To demonstrate how this works, let’s decompose it into two steps. Step 1 is to generate an index of sampling positions, and step 2 is to find those positions in your matrix:

    set.seed(1)
    mysample <- sample(length(dataset), n, replace=replace)
    mysample
     [1]  8 12 18 28  7 27 29 20 19  2
    
    dataset[mysample]
     [1] 1 0 0 1 0 1 1 0 0 1
    

    And, hey presto, the results of the two methods are identical.

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

Sidebar

Related Questions

I'm working with large CSV. How can I take a random sample of rows—say,
Is there anyway we can take input from command line in Objective-C, the way
can anybody tell How to take picture from front camera in android2.2 can anybody
The documentation for Array#sample says it can take an rng : If rng is
Can u give me a simple function in vb.net which can take number of
I'm trying to build a simple jquery plugin that can take selectors as parameters.
Can anyone show a short and simple code snippet? It should take the name
Simple as the title states: Can you use only Java commands to take a
PHP functions such as 'array_map' take a callback, which can be a simple function
I can take snapshot of a component. But the problem is the component is

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.