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

  • Home
  • SEARCH
  • 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 8168865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:39:55+00:00 2026-06-06T20:39:55+00:00

I am running a simulation that takes a sample of values in a matrix

  • 0

I am running a simulation that takes a sample of values in a matrix that starts with 1 column. I then put them through selection criteria and then from each row in the matrix it randomly selects a value from the output and saves that random selection. For some reason when I apply sample to the matrix on rows that have a real number and a NA, it returns a number that is not even available to be sampled. I may be doing something wrong with the sample function, but I don’t understand where this unknown value is coming from.

Example code:

theta <- c(30, 84, 159, 32, 60, 97)
omega <- 0.01
k <- 1
xn <- matrix(c(30, 84, 159, 32, 60, 97), ncol=1)

dup <- xn * 2 

set.seed(1)
z <- matrix(rbinom(n=rep(1,length(dup)),size = as.vector(dup),prob = 0.5),nrow = nrow(dup))            
z1 <- dup - z           
xn <- cbind(z, z1) # put both in a matrix
W <- exp( -(1/2)*( ( ( xn - theta ) / theta ) ^2 / omega ) )         

set.seed(1) 
Z <- matrix(rbinom(nrow(W) * ncol(W), 1, W), nrow=nrow(W), ncol=ncol(W) ) 
xn <- ifelse ( Z == 0, 0, xn )

xn
     [,1] [,2]
[1,]   32    0
[2,]   78    0
[3,]  144    0
[4,]    0   30
[5,]   60   60
[6,]   92  102

I don’t want to include any 0 values so I change them to NA and then apply the sample function to each row to return a single value.

xn[which(xn==0)] <- NA
set.seed(1)
xn2 <- matrix(apply(xn, 1, function(x){sample(x[!is.na(x)], size = k)}), ncol = k)

What I should get is

xn
     [,1]
[1,]   32 
[2,]   78 
[3,]  144 
[4,]   30
[5,]   60
[6,]  102

but what I get is:

xn
     [,1]
[1,]   9
[2,]   30
[3,]   83
[4,]   24
[5,]   60
[6,]  102

Specifically, in this example, the values 9, 23, 55, and 24 are coming out of nowhere that I know of.

Does anyone know what mistake I am making when I take this sample?

  • 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-06T20:39:57+00:00Added an answer on June 6, 2026 at 8:39 pm

    To summarize the comments,

    ?sample says

    If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x.

    For your application, when x is of length 1, you really just want to use the value of x instead of sample(x). You can adapt your code by adding a check to see if the length of x is greater than 1 before passing it through sample

    matrix(apply(xn, 1, function(x){
      if (length(x[!is.na(x)]) > 1) { 
        sample(x[!is.na(x)], size = k) 
      } else x[!is.na(x)] 
    }), ncol=k)
         [,1]
    [1,]   32
    [2,]   78
    [3,]  144
    [4,]   30
    [5,]   60
    [6,]  102
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running simulation code that is largely bound by CPU speed. I am
I have a simulation that uses OpenGL for drawing. While it is running, I
I'm running a long simulation in MATLAB that I've realized I need to stop
I am running a script that animates a plot (simulation of a water flow).
I have situation, where running a query that filters by an indexed column in
I have an iPhone app running in the simulator that won't quit. I also
I can detect that app is running under iOS Simulator (using sysctlbyname(hw.machine) ), but
I have a linux console application - a scientific simulation program that I use.
I am running a simulation trying to find the probability of something taking place
We are running an web application that is using Java 64bit 5 gigs of

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.