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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:54:46+00:00 2026-06-15T13:54:46+00:00

How can I randomly permute row keeping those with the same value adjacent to

  • 0

How can I randomly permute row keeping those with the same value adjacent to each other? I know that I can use randperm on the rows to randomly permute all rows, but I do not know how to keep the same-valued rows adjacent. Thanks.

A = [0 0 0;
     0 0 0;
     1 1 1;
     1 1 1;
     1 1 1;
     2 2 2;
     2 2 2];   

 permute_A = [0 0 0;
     0 0 0;
     2 2 2;
     2 2 2;
     1 1 1;
     1 1 1;
     1 1 1];
  • 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-15T13:54:47+00:00Added an answer on June 15, 2026 at 1:54 pm

    You have to identify the clusters and then permute them:

    Finding the clusters can be done by finding the changes in their differences:

    diffA = diff(A);
    clusters_start = [1 ; find(any(diffA,2)~=0)+1];
    

    Then the cluster ends are easily found by:

    clusters_end = [clusters_start(2:end)-1 ;size(A,1)];
    clusters_length = clusters_end-clusters_start+1;
    

    now you know the number of clusters and you can permute them:

    Nclusters = numel(clusters_start);
    perm_idx = randperm(Nclusters );
    clusters_start = clusters_start(perm_idx);
    clusters_end = clusters_end(perm_idx);
    clusters_length = clusters_length(perm_idx);
    

    and put them in a new matrix:

    newA = NaN(size(A));
    for ii=1:Nclusters
        newA(sum(clusters_length(1:ii-1))+(1:clusters_length(ii)),:) = A(clusters_start(ii):clusters_end(ii),:);
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a php function that can randomly select one row that has a
What is the simplest formula I can use to randomly choose a value in
Can anyone let me know how can we change the value of kendo combobox
How can I decompose a tree into forest such that each tree contains even
How can I randomly remove a key with value 0 efficiently ? Dictionary<string, int>
I'm looking to create something that can move randomly inside of a fixed div
How can I best write a query that selects 10 rows randomly from a
I know Guid are randomly generated, Is there any way I can find out
I want to fetch randomly record from a mysql table. I know we can
How can I randomly generate letters according to their frequency of use in common

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.