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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:05:18+00:00 2026-06-09T20:05:18+00:00

Basically, I’m trying to split a massive 1D vector into blocks of a given

  • 0

Basically, I’m trying to split a massive 1D vector into blocks of a given size which is passed through the function. The function should return a 2D vector and then I can just access the different blocks. I have found a suitable algoritm to do this, however, it is in Matlab and I do not understand how to place the elements inside the 2D vector.

MatLab Code:

function f = block(v, N, M)

n = length(v);
maxblockstart = n - N + 1;
lastblockstart = maxblockstart - mod(maxblockstart-1 , M);

numblocks = (lastblockstart-1)/M + 1;

f = zeros(numblocks,N);

for i = 1:numblocks
for j = 1:N
  f(i,j) = v((i-1)*M+j);
end
end

Here is my attempt in C++ (Sorry if it’s bad):

vector<iniMatrix> subBlocks(vector<int>& theData, int N, int M)
{
// This method splits the vector into blocks
// Each block has size N.
// and consecutive blocks differ 

int n = theData.size();
int maxblockstart = n - N+1;
int lastblockstart = maxblockstart - (maxblockstart-1 % M);

int numblocks = (lastblockstart-1)/M + 1;

vector<int> subBlock;
vector<iniMatrix> block;

for(unsigned i=0; (i < numblocks); i++)
{
    for(unsigned j=0; (j < N); j++)
    {
        subBlock.push_back(theData[(i-1*M+j)]);

        block.push_back(subBlock);
    }
}

return block;
}

This code compiles, but, when even trying to output the size of the block, i get: Segmentation fault: 11.. Any ideas?

The data being passed through the function is:
N = 600
M = 200

I hope someone can help me, thank you 🙂

  • 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-09T20:05:19+00:00Added an answer on June 9, 2026 at 8:05 pm

    In c and c++, array indices start from zero. This is important to keep in mind if you’re using array length functions. So, you should replace the i-1 multiplier with i and start the counting from zero. The loop condition:

    for (unsigned j=1; (j < N); j++)
    

    will start from 1 and end at the N-1 — a total of N-1 times. But,

    for (unsigned j=0; (j < N); j++)
    

    will start from 0 and end at N-1 — a total of N times.

    In Matlab: for-loops start from first boundary then end at second boundary

    If you feel that you must start from index 1,

    for (unsigned j=1; (j < N+1); j++)
    

    will do N iterations while still starting at 1. But please keep in mind that when you declare an array in C/C++, the index to the first element is zero.

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

Sidebar

Related Questions

Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I
Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
Basically from C++ FAQ I learned that: A virtual function allows derived classes to
Basically, what I'm trying to create is a page of div tags, each has
Basically what I'm trying to do is make a very simple vertical bullet projectile
Basically, I have a JTextPane to hold some text which I wish to style.
Basically I have converted a tab delimited txt file into a list containing a
Basically this function is meant to store the height value of the element that
basically I have a function that resizes elements accordingly with jquery triggering the function
Basically I have an IFRAME that contains a given page - I want to

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.