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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:38:54+00:00 2026-05-29T03:38:54+00:00

A question about matlab and randomisation of a 3d matrix respecting the rows and

  • 0

A question about matlab and randomisation of a 3d matrix respecting the rows and columns.

I have a n x n x s matrix M and I want to mess it up a bit, but with some control.

I can achieve my wish with a for loop

for j=1:size(M,3)
r=randperm(size(M,1));  
random_M(:,:,j)=M(r,r,j); 
end

Is there a way to perform this without having to loop over j? I need many randomisation iterations and could afford the benefits of indexing.

Cheers!

edit: Some more thoughts following Alexandrew’s comments

I have created a function that randomises a squeezed version of M:

function randomMat=randomiseMat(Mat)
[rows,cols]=size(Mat);
r=randperm(rows);
randomMat=Mat(r,r);

then, using arrayfun I seem to get what I want:

randomM=arrayfun(@(x) randomiseMat(M(:,:,x)),1:size(M,3),'UniformOutput', false)

however, randomM is now a cell array of size (1,size(M,3)) with each cell containing randomised array.

Is there a way to make it in a 3d matrix just like the input M?

  • 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-29T03:38:55+00:00Added an answer on May 29, 2026 at 3:38 am

    You can calculate all the values for r in one go, and then use arrayfun:

    [nRows,nCols,nPages] = size(M);
    [~,r]=sort(rand(nRows,nPages));
    
    %# you should test on a realistic example whether a for-loop
    %# isn't faster here
    outCell = arrayfun(@(x) M(r(:,x),r(:,x),x), 1:nPages,'UniformOutput',false);
    randomM = cat(3,outCell{:});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to Matlab and have a question about creating a logical
Question about subclassing in matlab, under the new class system. I've got class A
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
This question about Timers for windows services got me thinking: Say I have (and
Quick question about Wordpress PHP: I am writing a theme and I want to
hopefully this is a basic question about make pattern rules: I want to use
I have had trouble finding help in the matlab documentation and previous questions about
I have a question about the values returned by getPosition. Below is my code.
Question about threading to satisfy my curiosity... Let's say I have static variable _status
Question about OO design. Suppose I have a base object vehicle. And two descendants:

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.