MATLAB: Sum & Remove Array Entries (0, 1 & >1)

In summary, the conversation discusses finding and removing certain entries in a matrix and summing the values of those entries. The suggested solutions involve using the "find" and "length" functions to locate and count the desired entries, as well as using the "max" function to remove entries greater than 1 in the matrix. However, there is a concern about the speed of the suggested solution and a request for potential improvements.
  • #1
hoffmann
70
0
Hi,

How can I find the following from an array with entries of 0, 1, and >1:

Find the sum of all the entries in the matrix that are not equal to 0.
Find the sum of all the entries in the matrix.
Find the sum of all the zero and 1 entries in the matrix.
How to remove the entries in the matrix that are not equal to 0, one by one.

Thanks!
 
Physics news on Phys.org
  • #2
I guess I can simplify my question:

I have a matrix A that contains values of 0, 1, and values that are greater than one. I need to remove the entries in the matrix that are greater than 1 until the number of values that are 0 and 1 outnumber the number of values that are greater than 1. Then I need to display the new matrix B that contains the result of the aforementioned operation.

Hope that helps and thanks in advance!
 
  • #3
Alright, so I have this so far:

function[newInteractions]=prune(interactions)

while( sum(interactions(interactions~=0)) > sum(interactions(interactions<=1)) )

i=max(interactions);

interactions(i)=[];

end

newInteractions=interactions;

I have a feeling that this can be improved in terms of speed -- the max function seems to be very slow. Can this be improved?
 
  • #4
Hi

If you array is something like:

A=[1 0 0 0 1 0 1 0 25 5 8 69 0 0 1 0 1 0 1 1 1 1 0 4 7 0 1]

A quick way to remove all the bumf greater than 1 in your array is:

A(A>1)=[];

______

For your summing problems try:

x=find(A>1) %This will return all the points greater than 1 in your data.
y=length(x) %This will give you a number corresponding to the number of successful finds.
 

What is MATLAB?

MATLAB is a programming language and interactive environment commonly used by scientists, engineers, and mathematicians for data analysis, visualization, and numerical computation.

What is the purpose of the "Sum & Remove Array Entries" function?

The "Sum & Remove Array Entries" function in MATLAB is used to calculate the sum of all elements in an array and remove any entries that are equal to 0, 1, or greater than 1.

How do I use the "Sum & Remove Array Entries" function in MATLAB?

To use the "Sum & Remove Array Entries" function in MATLAB, you must first define an array of numbers. Then, use the syntax "array = sumRemoveEntries(array)" to apply the function to your array.

Can I use this function for arrays of any size?

Yes, the "Sum & Remove Array Entries" function in MATLAB can be used for arrays of any size, including one-dimensional and multi-dimensional arrays.

What happens if my array contains non-numeric values?

If your array contains non-numeric values, the "Sum & Remove Array Entries" function will return an error. It is important to ensure that your array only contains numeric values before using this function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
766
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
729
Replies
1
Views
548
  • Linear and Abstract Algebra
Replies
2
Views
608
  • Linear and Abstract Algebra
Replies
2
Views
463
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top