MATLAB Solve Large Sorted Matrix in MATLAB

  • Thread starter Thread starter hoffmann
  • Start date Start date
  • Tags Tags
    Matlab Matrix
AI Thread Summary
To solve the problem of balancing entries in a sorted matrix in MATLAB, the goal is to ensure that the count of entries greater than one equals the count of entries less than or equal to one. The proposed algorithm involves summing the counts of two types of entries: those less than or equal to one (type A) and those greater than one (type B). By iteratively removing the last entry of the matrix and recalculating the counts, one can determine when the counts of type A and type B are equal. This approach can be applied to larger datasets as well. Implementing this logic in MATLAB will allow for efficient handling of the matrix adjustments.
hoffmann
Messages
65
Reaction score
0
I have restated my question even further:

Suppose I have a sorted matrix:

[ 0 0 0 1 1 1 1 3 3 3 5 6 7 8 9 9 ]

I need to remove the last value in the matrix until the number of entries that are greater than one equal the number of entries that are either 0 or 1. For the example given above, there are 16 entries. Nine entries are greater than 1 and 7 entries are less than or equal to one. I would need to remove the last two entries such that the number of entries that are greater than one equal the number of entries that are either 0 or 1.

What if I have a much larger set of values? How do code this in MATLAB?

Thanks!
 
Physics news on Phys.org
I think the algorithm's can be like this:
  1. sum the numbers of type A and type B in your string, for example, type A means if x<=1,and type B means if x>1, and after that, we get the number of A(NoA) and number of B(NoA) in your string.
  2. move the pointer from the last entry,and remove these last entries, and caculate the remaining number of A(RoA) and number of B(RoB), you know, it's very simple to get the number of A and B you removed.
  3. Each time, you remove the last entry and check if RA==RB, then you get the solution.

I hope you can understand my algorithm.
 

Similar threads

Back
Top