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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:41:22+00:00 2026-06-08T12:41:22+00:00

I have matrix A A= [0 0 2 2 2 2 0 0 1

  • 0

I have matrix A

A= [0 0 2 2 2 2 0 0 1 1 1 0 3 3;
    2 2 2 2 0 0 1 1 1 0 0 3 3 0;

As you can see, there are consecutive numbers in it; notice for example the 2 2 2 2 on the first and second row.

For each number occuring in this matrix (or at least for every number from 1 to the maximum number in my matrix) I want to have an output matrix that indicates sequences of this number and this number only in the original matrix.

So for example, for 1: there are three consecutive numbers on the first row and three on the second row: I want to indicate this in the first output matrix as follows:

Matrix 1 = [ 0 0 0 0 0 0 0 0 1 2 3 0 0 0;
             0 0 0 0 0 0 0 1 2 3 0 0 0 0]

Same for number 2:

Matrix 2 = [ 0 0 1 2 3 4 0 0 0 0 0 0 0 0;
             1 2 3 4 0 0 0 0 0 0 0 0 0 0]

and 3:

Matrix 3 = [ 0 0 0 0 0 0 0 0 0 0 0 0 1 2;
             0 0 0 0 0 0 0 0 0 0 0 1 2 0]

As you can see, each output matrix shows counting forward for the consecutive occurrences of a number.

So in this case, I have 3 output matrices because matrix A has 3 as the biggest value there.

  • 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-08T12:41:24+00:00Added an answer on June 8, 2026 at 12:41 pm

    You can try this:

    A= [0 0 2 2 2 2 0 0 1 1 1 0 3 3;
        2 2 2 2 0 0 1 1 1 0 0 3 3 0];
    
    result = arrayfun(@(b) (A == b).*cumsum((A == b),2),nonzeros(unique(A)), 'UniformOutput', false);
    

    For this example, there will be 3 submatrices in the variable result.

    result = 
    
        [2x14 double]
        [2x14 double]
        [2x14 double]
    

    To access them, use the following syntax:

    result{1}
    result{2}
    result{3}
    

    Then you get:

    ans =
    
         0     0     0     0     0     0     0     0     1     2     3     0     0     0
         0     0     0     0     0     0     1     2     3     0     0     0     0     0
    
    
    ans =
    
         0     0     1     2     3     4     0     0     0     0     0     0     0     0
         1     2     3     4     0     0     0     0     0     0     0     0     0     0
    
    
    ans =
    
         0     0     0     0     0     0     0     0     0     0     0     0     1     2
         0     0     0     0     0     0     0     0     0     0     0     1     2     0
    

    ~edit~
    If, as asked in the comments, A is a 3D matrix, this code works just the same, but the structure of result is a bit different:

    result = 
        [2x14x2 double]
        [2x14x2 double]
        [2x14x2 double]
    

    To access these matrices, use for instance

    result{1}(:,:,1) % for the results of comparing A(:,:,1) with value 1
    result{1}(:,:,2) % for the results of comparing A(:,:,2) with value 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a matrix of weights, and another matrix of data values. Can
I have a datset, which is dat<-matrix(seq(1:5290),c(115,46)) How can I convert dat to a
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix of data (the columns represent time, and the rows spectrum
I have the matrix m <- matrix(c(1, 0, 3, 4, 0, 6), 3) I
I have a matrix A which I want to convert into a data.frame of
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix in SAS/IML: x = {7 6 3 3 8, 2
I have a matrix in the type of a Numpy array. How would I

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.