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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:38:59+00:00 2026-06-14T22:38:59+00:00

I have an array A, and want to reshape the last four elements of

  • 0

I have an array A, and want to reshape the last four elements of each column into a 2×2 matrix. I would like the results to be stored in a cell array B.

For example, given:

A = [1:6; 3:8; 5:10]';

I would like B to contain three 2×2 arrays, such that:

B{1} = [3, 5; 4, 6];
B{2} = [5, 7; 6, 8];
B{3} = [7, 9; 8, 10];

I can obviously do this in a for loop using something like reshape(A(end-3:end, ii), 2, 2) and looping over ii. Can anyone propose a vectorized method, perhaps using something similar to cellfun that can apply an operation repeatedly to columns of an array?

  • 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-14T22:39:01+00:00Added an answer on June 14, 2026 at 10:39 pm

    The way I do this is to look at the desired indices and then figure out a way to generate them, usually using some form of repmat. For example, if you want the last 4 items in each column, the (absolute) indices into A are going to be 3,4,5,6, then add the number of rows to that to move to the next column to get 9,10,11,12 and so on. So the problem becomes generating that matrix in terms of your number of rows, number of columns, and the number of elements you want from each column (I’ll call it n, in your case n=4).

    octave:1>  A = [1:6; 3:8; 5:10]'
    A =
    
        1    3    5
        2    4    6
        3    5    7
        4    6    8
        5    7    9
        6    8   10
    
    octave:2>  dim=size(A)
    dim =
    
       6   3
    
    octave:3> n=4
    n =  4
    octave:4> x=repmat((dim(1)-n+1):dim(1),[dim(2),1])'
    x =
    
       3   3   3
       4   4   4
       5   5   5
       6   6   6
    
    octave:5> y=repmat((0:(dim(2)-1)),[n,1])
    y =
    
       0   1   2
       0   1   2
       0   1   2
       0   1   2
    
    octave:6> ii=x+dim(1)*y
    ii =
    
        3    9   15
        4   10   16
        5   11   17
        6   12   18
    
    octave:7> A(ii)
    ans =
    
        3    5    7
        4    6    8
        5    7    9
        6    8   10
    
    octave:8> B=reshape(A(ii),sqrt(n),sqrt(n),dim(2))
    B =
    
    ans(:,:,1) =
    
       3   5
       4   6
    
    ans(:,:,2) =
    
       5   7
       6   8
    
    ans(:,:,3) =
    
        7    9
        8   10
    

    Depending on how you generate x and y, you can even do away with the multiplication, but I’ll leave that to you. 😀

    IMO you don’t need a cell array to store them either, a 3D matrix works just as well and you index into it the same way (but don’t forget to squeeze it before you use it).

    I gave a similar answer in this question.

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

Sidebar

Related Questions

I have an array I want to check the the last digits if it
I have array populated with elements and I want to display elements of that
I have an array and I want to divide them into page according to
i have an array i want to join into a string using comma(,) separated
I have an array and I want to remove some elements. I tried this
I have an array of strings: @array I want to concatenate all strings beginning
i have an array and i want to convert this array in comma seprated
I have an array which I want to use in a query: Array (
I have to array and I want to merge this two array with a
I have an array that I want to sort based on the value of

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.