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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:25:38+00:00 2026-05-14T15:25:38+00:00

I have a large matrix from which I would like to gather a collection

  • 0

I have a large matrix from which I would like to gather a collection of submatrices. If my matrix is NxN and the submatrix size is MxM, I want to collect I=(N - M + 1)^2 submatrices. In other words I want one MxM submatrix for each element in the original matrix that can be in the top-left corner of such a matrix.

Here’s the code I have:

for y = 1:I
    for x = 1:I
        index = (y - 1) * I + x;
        block_set(index) = big_mat(x:x+M-1, y:y+M-1)
    endfor
 endfor

The output if a) wrong, and b) implying there is something in the big_mat(x:x+M-1, y:y+M-1) expression that can get me what I want without needing the two for loops. Any help would be much appreciated

  • 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-14T15:25:39+00:00Added an answer on May 14, 2026 at 3:25 pm

    There seem to be a few things wrong in your code. Here’s how I’d do it if I were to use the double loop:

    M = someNumber;
    N = size(big_mat,1); %# I assume big_mat is square here
    
    %# you need different variables for maxCornerCoord and nSubMatrices (your I)
    %# otherwise, you are going to index outside the image in the loops!
    maxCornerCoord = N-M+1;
    nSubMatrices = maxCornerCoord^2;
    
    %# if you want a vector of submatrices, you have to use a cell array...
    block_set = cell(nSubMatrices,1); 
    %# ...or a M-by-M-by-nSubMatrices array...
    block_set = zeros(M,M,nSubMatrices);
    %# ...or a nSubMatrices-by-M^2 array
    block_set = zeros(nSubMatrices,M^2);
    
    for y = 1:maxCornerCoord
        for x = 1:maxCornerCoord
            index = (y - 1) * maxCornerCoord + x; 
            %# use this line if block_set is a cell array
            block_set{index} = big_mat(x:x+M-1, y:y+M-1);
            %# use this line if block_set is a M-by-M-by-nSubMatrices array
            block_set(:,:,index) = big_mat(x:x+M-1, y:y+M-1);
            %# use this line if block_set is a nSubMatrices-by-M^2 array
            block_set(index,:) = reshape(big_mat(x:x+M-1, y:y+M-1),1,M^2);
        endfor
     endfor
    

    EDIT

    I just saw that there is an implementation of im2col for Octave. Thus, you can rewrite the double-loop as

    %# block_set is a M^2-by-nSubMatrices array
    block_set = im2col(big_mat,[M,M],'sliding');
    
    %# if you want, you can reshape the result to a M-by-M-by-nSubMatrices array
    block_set = reshape(block_set,M,M,[]);
    

    This is probably faster, and saves lots of digital trees.

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

Sidebar

Related Questions

I have a large matrix from which I would like to randomly extract a
I have a large m *n sparse matrix Y. I would like to normalize
I have a large matrix that I would like to center: X <- matrix(sample(1:10,
I have a large matrix that I would like to convert to sparse CSR
I have large images displayed in a grouped tableview. I would like the images
I have a large matrix (2e6 x 3) which I have to write to
I have a large scipy.sparse.csc_matrix and would like to normalize it. That is subtract
I'm new to python, coming from matlab. I have a large sparse matrix saved
I have a large matrix of 0 s and 1 s, that is mostly
I have large set of flow charts and workflow diagrams. I want to draw

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.