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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:24:19+00:00 2026-05-27T08:24:19+00:00

I have a large matrix from which I would like to randomly extract a

  • 0

I have a large matrix from which I would like to randomly extract a smaller matrix. (I want to do this 1000 times, so ultimately it will be in a for loop.) Say for example that I have this 9×9 matrix:

mat=matrix(c(0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,
          0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,
          1,0,1,0,0,0,0,0,1,0,1,0,0,0,1), nrow=9)

From this matrix, I would like a random 3×3 subset. The trick is that I do not want any of the row or column sums in the final matrix to be 0. Another important thing is that I need to know the original number of the rows and columns in the final matrix. So, if I end up randomly selecting rows 4, 5, and 7 and columns 1, 3, and 8, I want to have those identifiers easily accessible in the final matrix.

Here is what I’ve done so far.

First, I create a vector of row numbers and column numbers. I am trying to keep these attached to the matrix throughout.

r.num<-seq(from=1,to=nrow(mat),by=1)      #vector of row numbers
c.num<-seq(from=0, to=(ncol(mat)+1),by=1) #vector of col numbers (adj for r.num)

mat.1<-cbind(r.num,mat)
mat.2<-rbind(c.num,mat.1)

Now I have a 10×10 matrix with identifiers. I can select my rows by creating a random vector and subsetting the matrix.

rand <- sample(r.num,3)
temp1 <- rbind(mat.2[1,],mat.2[rand,])      #keep the identifier row

This works well! Now I want to randomly select 3 columns. This is where I am running into trouble. I tried doing it the same way.

rand2 <- sample(c.num,3)
temp2 <- cbind(temp1[,1],temp1[,rand2])

The problem is that I end up with some row and column sums that are 0. I can eliminate columns that sum to 0 first.

temp3 <- temp1[,which(colSums(temp1[2:nrow(temp1),])>0)]
cols <- which(colSums(temp1[2:nrow(temp1),2:ncol(temp1)])>0)
rand3 <- sample(cols,3)
temp4 <- cbind(temp3[,1],temp3[,rand3])

But I end up with an error message. For some reason, R does not like to subset the matrix this way.

So my question is, is there a better way to subset the matrix by the random vector “rand3” after the zero columns have been removed OR is there a better way to randomly select three complementary rows and columns such that there are none that sum to 0?

Thank you so much for your help!

  • 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-27T08:24:20+00:00Added an answer on May 27, 2026 at 8:24 am

    If I understood your problem, I think this would work:

    mat=matrix(c(0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,
              0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,
              1,0,1,0,0,0,0,0,1,0,1,0,0,0,1), nrow=9)
    
    smallmatrix = matrix(0,,nrow=3,ncol=3)
    
     while(any(apply(smallmatrix,2,sum) ==0) | any(apply(smallmatrix,1,sum) ==0)){
          cols = sample(ncol(mat),3)
          rows= sample(nrow(mat),3)
          smallmatrix = mat[rows,cols]
    }
    
    colnames(smallmatrix) = cols
    rownames(smallmatrix) = rows
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large matrix from which I would like to gather a collection
I have large images displayed in a grouped tableview. I would like the images
Hi I have been working on a large matrix which displays the results of
let's say I have a matrix (array) like this example, but much larger: 0
I have a very large matrix(10x55678) in numpy matrix format. the rows of this
I'm new to python, coming from matlab. I have a large sparse matrix saved
I need to read a large (2000x2000) matrix of binary data from a file
I have a matrix which can easily be displayed as an image in R
I have a large matrix M implemented as vector<vector<double> with m rows, i.e. the
I'm working with a large matrix (250x250x30 = 1,875,000 cells), and I'd like a

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.