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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:55:12+00:00 2026-05-30T22:55:12+00:00

I have a 32 x 3 matrix as follows: A = [ 1 1

  • 0

I have a 32 x 3 matrix as follows:

A =

[ 1 1 1;

1 1 2;

1 1 3;

1 1 4;

1 1 5;

1 1 6;

1 1 7;

1 1 8;

1 2 1;

1 2 2;

1 2 3;

1 2 4;

1 2 5;

1 2 6;

1 2 7;

1 2 8;

2 1 1;

2 1 2;

2 1 3;

2 1 4;

2 1 5;

2 1 6;

2 1 7;

2 1 8;

2 2 1;

2 2 2;

2 2 3;

2 2 4;

2 2 5;

2 2 6;

2 2 7;

2 2 8]

What I need to do is randomise the order of the rows, while keeping the row values together, however, this needs to be constrained such that for A(:, 4), every 8 rows contain only the numbers 1 – 8. So for example, you could have something like:

A(1:8, 🙂 =

[ 1 2 4;

1 1 5;

2 1 6;

1 1 8;

2 2 1;

2 1 2;

2 1 7;

1 1 3]

The occurrence of 1 and 2 in the first two columns needs to be random, 1 – 8 needs to be randomised for every 8 values of the third column. Initially I tried to use the function randswap within a loop with an added constraint, but this has only led to an infinite loop. Also important that the rows stay together as the 1s and 2s in the first two columns need to appear alongside the last column an equal number of times. Someone suggested the following, but it doesn’t quite work out..

  m = size(A,1);

  n = 1:8;

  out = 1;

  i2 = 1;

  while ~all(ismember(1:8,out)) && i2 < 100

   i1 = randperm(m);

   out = A(i1(n),:);

   i2 = i2 + 1;

  end
  • 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-30T22:55:14+00:00Added an answer on May 30, 2026 at 10:55 pm

    If all you need is to get one set of 8 rows out of A, you can construct the result like this:

    out = [randi([1,2],[8,2]),randperm(8)']
    
    out =
         2     1     5
         2     1     2
         2     1     1
         1     2     7
         2     2     6
         1     1     8
         2     2     3
         1     1     4
    

    If you need to randomize all of A, you can do the following:

    %# calculate index for the 1's and 2's
    r = rand(8,4);
    [~,idx12] = sort(r,2);
    
    %# calculate index for the 1's through 8's
    [~,idx8] = sort(r,1);
    
    %# use idx8 to shuffle idx12
    idx8into12 = bsxfun(@plus,idx8,[0 8 16 24]);
    
    %# now we can construct the output matrix
    B = [1 1;1 2;2 1;2 2];
    out = [B(idx12(idx8into12),:),idx8(:)];
    
    out =
         1     1     7
         1     2     3
         1     2     1
         2     1     8
         2     1     5
         2     2     4
         2     2     2
         2     1     6
         1     1     3
         1     2     7
         1     1     1
         1     2     8
         1     2     4
         1     1     5
         2     2     6
         2     1     2
         1     1     8
         2     1     7
         1     2     5
         2     1     1
         1     2     6
         2     1     3
         1     1     2
         1     1     4
         2     1     4
         2     2     7
         2     2     8
         2     2     3
         1     2     2
         1     1     6
         2     2     5
         2     2     1
    

    If you do unique(out,'rows'), you’ll see that there are indeed 32 unique rows. Every 8 rows in out have the numbers 1 through 8 in the third column.

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

Sidebar

Related Questions

I need some help in this problem I have this matrix in MATLAB: A
Suppose I have a matrix foo as follows: foo <- cbind(c(1,2,3), c(15,16,17)) > foo
I have a matrix in MATLAB and I need to find the 99% value
I have defined a Matrix module as follows: module Matrix = struct type 'a
I have a list of matrices, the first matrix of which looks as follows:
I have a matrix as follows `> y 1 2 3 1 0.8802216 1.2277843
I have a matrix. And I need to get 1D arrays from my matrix.
I have matrix as follows.. 31348 439352 6077 4619722 60825 31348 1 0.304 0.126
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix in SQL reporting and I would like it to print

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.