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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:41:15+00:00 2026-05-28T07:41:15+00:00

I have vector with values between 1 and N > 1 . Some values

  • 0

I have vector with values between 1 and N > 1. Some values COULD occur multiple times consecutively. Now I want to have a second row which counts the consecutively entries and remove all those consecutively occuring entries, e.g.:

A = [1 2 1 1 3 2 4 4 1 1 1 2]'

would lead to:

B = [1 1;
     2 1;
     1 2;
     3 1;
     2 1;
     4 2;
     1 3;
     2 1]

(you see, the second column contains the number of consecutively entries!
I came across accumarray() in MATLAB recently but I can’t find any solution with it for this task since it always regards the whole vector and not only consecutively entries.

Any idea?

  • 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-28T07:41:16+00:00Added an answer on May 28, 2026 at 7:41 am

    This probably isn’t the most readable or elegant way of doing it, but if you have large vectors and speed is an issue, this vectorisation may help…

    A = [1 2 1 1 3 2 4 4 1 1 1 2];
    

    First I’m going to pad A with a leading and trailing zero to capture the first and final transitions

    >>  A = [0, A, 0];
    

    The transition locations can be found where the difference between neighbouring values is not equal to zero:

    >> locations = find(diff(A)~=0);
    

    But because we padded the start of A with a zero, the first transition is nonsensical, so we only take the locations from 2:end. The values in A of these are the value of each segment:

    >> first_column = A(locations(2:end))
    
    ans =
    
         1     2     1     3     2     4     1     2
    

    That’s the first colomn – now to find the count of each number. This can be found from the difference in locations. This is where padding A at both ends becomes important:

    >> second_column = diff(locations)
    
    ans =
    
     1     1     2     1     1     2     3     1
    

    Finally combining:

    B = [first_column', second_column']
    
    B =
    
     1     1
     2     1
     1     2
     3     1
     2     1
     4     2
     1     3
     2     1
    

    This can all be combined into one less-readable line:

    >> A = [1 2 1 1 3 2 4 4 1 1 1 2]';
    >> B = [A(find(diff([A; 0]) ~= 0)), diff(find(diff([0; A; 0])))]
    
    B =
    
     1     1
     2     1
     1     2
     3     1
     2     1
     4     2
     1     3
     2     1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector with int values like: v=[10,8,6] and what I want is
I have a vector of ~100k length, with values between 0 and 1 representing
I have a vector of numbers between 1 and 100(this is not important) which
I have a vector containing integer values, which I wish to sum. However, it's
I have a vector containing the values 0, 1, 2 and 3. What I
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
I have a vector that I want to insert into a set . This
I have a vector of integers and I want to convert it to a
I have a vector of beans that holds information I want to display in
Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int

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.