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 database and would like to select table names that have
I have a large text template which needs tokenized sections replaced by other text.
I have a large CSV file and I want to execute a stored procedure
I have large data sets (10 Hz data, so 864k points per 24 Hours)
I have large batches of XHTML files that are manually updated. During the review
I'm developing a website in PHP and I have large JS files that I
I have a large tree of Java Objects in my Desktop Application and am
I have a large codebase that targetted Flash 7, with a lot of AS2
I have a large table with 1 million+ records. Unfortunately, the person who created
I have a large application (~50 modules) using a structure similar to the following:

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.