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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:18:22+00:00 2026-06-08T06:18:22+00:00

I need to circularly shift individual columns of a matrix. This is easy if

  • 0

I need to circularly shift individual columns of a matrix.

This is easy if you want to shift all the columns by the same amount, however, in my case I need to shift them all by a different amount.

Currently I’m using a loop and if possible I’d like to remove the loop and use a faster, vector based, approach.

My current code

A = randi(2, 4, 2);
B = A;
for i = 1:size( A,2 );
   d = randi( size( A,1 ));
   B(:,i) = circshift( A(:,i), [d, 0] );
end

Is is possible to remove the loop from this code?

Update I tested all three methods and compared them to the loop described in this question. I timed how long it would take to execute a column by column circular shift on a 1000×1000 matrix 100 times. I repeated this test several times.

Results:

  • My loop took more than 12 seconds
  • Pursuit’s suggestion less than a seconds
  • Zroth’s orginal answer took just over 2 seconds
  • Ansari’s suggest was slower than the original loop
  • 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-08T06:18:23+00:00Added an answer on June 8, 2026 at 6:18 am

    Edit

    Pursuit is right: Using a for-loop and appropriate indexing seems to be the way to go here. Here’s one way of doing it:

    [m, n] = size(A);
    D = randi([0, m - 1], [1, n]);
    B = zeros(m, n);
    
    for i = (1 : n)
        B(:, i) = [A((m - D(i) + 1 : m), i); A((1 : m - D(i) ), i)];
    end
    

    Original answer

    I’ve looked for something similar before, but I never came across a good solution. A modification of one of the algorithms used here gives a slight performance boost in my tests:

    [m, n] = size(A);
    mtxLinearIndices ...
        = bsxfun(@plus, ...
                 mod(bsxfun(@minus, (0 : m - 1)', D), m), ...
                 (1 : m : m * n));
    C = A(idxs);
    

    Ugly? Definitely. Like I said, it seems to be slightly faster (2–3 times faster for me); but both algorithms are clocking in at under a second for m = 3000 and n = 1000 (on a rather old computer, too).

    It might be worth noting that, for me, both algorithms seem to outperform the algorithm provided by Ansari, though his answer is certainly more straightforward. (Ansari’s algorithm’s output does not agree with the other two algorithms for me; but that could just be a discrepancy in how the shifts are being applied.) In general, arrayfun seems pretty slow when I’ve tried to use it. Cell arrays also seem slow to me. But my testing might be biased somehow.

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

Sidebar

Related Questions

Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
Need a map reduce function by mongo in php This my mongo structure [_id]
Need just a push in the right direction with this. I'm building a multi-language
I need to display a tree bound to this kind of hierarchical objects: public
I need to perform a circular left shift of a 64-bit integer in JavaScript.
Good day to all. I need to create a graphical countdown in js. The
( I tagged this as both C# and Java, since it's the same question
I need help on implementing a circular progress bar like this: How should I
I need to circular shift some characters/bit using Perl For example, with the following
I want to create a round circular button. This button should look 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.