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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:45:09+00:00 2026-06-11T15:45:09+00:00

I have a matrix which has the following form: M = [1 4 56

  • 0

I have a matrix which has the following form:

M = 
[1 4 56 1;
 1 3 5  1;
 1 3 6  4;
 2 3 5  0;
 2 0 0  0;
 3 1 2  3;
 3 3 3  3]

I want to split this matrix based on the number given in the first column. So I want to split the matrix into this:

A = 
[1 4 56 1;
 1 3 5  1;
 1 3 6  4]

B = 
[2 3 5  0;
 2 0 0  0]

C =
[3 1 2  3;
 3 3 3  3]

I tried this by making the following loop, but this gave me the desired matrices with rows of zeros:

for i = 1:length(M)
    if (M(i,1) == 1)
        A(i,:) = M(i,:);
    elseif (M(i,1) == 2)
        B(i,:) = M(i,:);
    elseif (M(i,1) == 3)
        C(i,:) = M(i,:);
    end
end

The result for matrix C is then for example:

C = 
[0 0 0 0;
 0 0 0 0;
 0 0 0 0;
 2 3 5 0;
 2 0 0 0]

How should I solve this issue?

Additional information:
The actual data has a date in the first column in the form yyyymmdd. The data set spans several years and I want to split this dataset in matrices for each year and after that for each month.

  • 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-11T15:45:11+00:00Added an answer on June 11, 2026 at 3:45 pm

    You can use arrayfun to solve this task:

    M = [
    1 4 56 1;
     1 3 5  1;
     1 3 6  4;
     2 3 5  0;
     2 0 0  0;
     3 1 2  3;
     3 3 3  3]
    
    
    A = arrayfun(@(x) M(M(:,1) == x, :), unique(M(:,1)), 'uniformoutput', false)
    

    The result A is a cell array and its contents can be accessed as follows:

    >> a{1}
    
    ans =
    
         1     4    56     1
         1     3     5     1
         1     3     6     4
    
    >> a{2}
    
    ans =
    
         2     3     5     0
         2     0     0     0
    
    >> a{3}
    
    ans =
    
         3     1     2     3
         3     3     3     3
    

    To split the data based on an yyyymmdd format in the first column, you can use the following:

    yearly = arrayfun(@(x) M(floor(M(:,1)/10000) == x, :), unique(floor(M(:,1)/10000)), 'uniformoutput', false)
    
    monthly = arrayfun(@(x) M(floor(M(:,1)/100) == x, :), unique(floor(M(:,1)/100)), 'uniformoutput', false)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix A which I want to convert into a data.frame of
I am working with a MxM triangular matrix which has the following form: M
So I have a generic Matrix class that I created which has the following
Possible Duplicate: correlation matrix in r I have an excel sheet which has 700
I have a 4x4 matrix which I wish to decompose into 4 frequency bands
I have a matrix which has multiple rows per ID. I need to extract
I have a simple matrix class which has a 2d integer pointer field in
I want to multiply a sparse matrix A, with a matrix B which has
Suppose I have a column matrix pols containing vectors of [theta, rho, z]. Which
I have got a matrix which gives me a 2-dimensional discrete distribution (N²->R) in

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.