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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:52:28+00:00 2026-06-10T06:52:28+00:00

If I have a matrix: 0 0 3 4 4 3 2 0 2

  • 0

If I have a matrix:

0   0   3   4  
4   3   2   0  
2   0   2   0  

I want to extract the non-zero elements into some batches, with respect to a rule: if an element is already taken, the other element in the same row/column is not allowed. So the extracted matrix will be:
1st batch:

0   0   3   0  
4   0   0   0  
0   0   0   0  

2nd batch:

0   0   0   4  
0   3   0   0  
0   0   2   0  

3rd batch:

0   0   0   0  
0   0   2   0  
2   0   0   0  

Any other combination of batches is also accepted, as long as all non-zero elements are covered, and the rule is conformed. How would you do that in MATLAB/Octave?

  • 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-10T06:52:29+00:00Added an answer on June 10, 2026 at 6:52 am

    Gunther was already on the right track. You want to select an element, if

    1. the row cumsum of the non-zeros is 1 AND
    2. the column cumsum of the non-zeros is 1 AND
    3. the element itself is non-zero.

    The following code solves the problem:

    A = [0, 0, 3, 4;
         4, 3, 2, 0;
         2, 0, 2, 0];
    
    batches = cell(0);
    while any(A(:)~=0)
        selector = cumsum(A~=0, 1) .* cumsum(A~=0, 2) .* (A~=0) == 1;
        batches{end+1} = A .* selector;
        A(selector) = 0;
    end
    

    Note however that the returned solution is not optimal because its 2nd batch is

    0   0   0   4  
    0   3   0   0  
    2   0   0   0 
    

    which means that the remaining matrix elements are from the same column:

    0   0   0   0
    0   0   2   0  
    0   0   2   0 
    

    Unfortunately, you cannot draw them in the same batch. So you end up with four batches instead of just three.

    Edit: Probably, it is a good idea, to select first those elements, which appear in rows/columns with a lot of non-zeros. For example, one could use these weights

    weight = repmat(sum(A~=0, 1), size(A, 1), 1) ...
             .* repmat(sum(A~=0, 2), 1, size(A, 2)) .* (A~=0)
    
    weight =
         0     0     6     2
         6     3     9     0
         4     0     6     0
    

    The following algorithm

    batches = cell(0);
    while any(A(:)~=0)
        batch = zeros(size(A));
        weight = repmat(sum(A~=0, 1), size(A, 1), 1) ...
                 .* repmat(sum(A~=0, 2), 1, size(A, 2)) .* (A~=0);
        while any(weight(:)~=0)
            [r,c] = find(weight == max(weight(:)), 1);
            batch(r,c) = A(r,c);
            A(r,c) = 0;
            weight(r,:) = 0;
            weight(:,c) = 0;
        end
        batches{end+1} = batch;
    end
    

    returns those batches.

    batches{:}
    ans =
         0     0     0     4
         0     0     2     0
         2     0     0     0
    
    ans =
         0     0     3     0
         4     0     0     0
         0     0     0     0
    
    ans =
         0     0     0     0
         0     3     0     0
         0     0     2     0
    

    So it worked at least for this small test case.

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

Sidebar

Related Questions

I have a NxN matrix which I want to split into non-overlap KxK block.
I have a matrix A which I want to convert into a data.frame of
I have this matrix of size 10000 by 1. I want to remove some
So I have 2d matrix and I want to extract every fifth value from
I have a two dimensional array having elements of a matrix I want to
I have a matrix of object and I want to get an element of
I have a non-fixed dimensional matrix M, from which I want to access a
I have a matrix A consisting of 200 vectors of size d. I want
so we have this matrix a=[1;2;3] and we want to multiply it by itself
I have this following numpy matrix that I want to sort in ascending order

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.