Sum of a list of numbers if they're a certain value or not

I have a list of numbers, and I'm trying to determine the sum of all of the numbers that have a value less than 55. I know that this will involve some sort of logic set-up, but I'm not familiar with how to set it up exactly. Any help is really appreciated.

 Risposta accettata

Or even (without using sum)
x.'*(x<55)
[edit]
for x being a matrix
x = randi(100,20,20);
sx = x(:).'*(x(:)<55);

4 Commenti

This returns several rows and columns of values; obviously, the values that are below 55. However, how do I get the sum of these without using summation?
I assumed the numbers were a column. You can change a matrix to a column by doing x=x(:)
Thank you! Worked like a charm. Can you explain what the code is doing though? I really appreciate it.
reading from left to right the line x(:).'*(x(:)<55);
x(:) returns all elements of x in a column (see doc colon)
.' transposes x(:)from column to row
* is matrix multiply
(x(:)<55) returns a column of 1's & 0's (1 is true x(n),55)
in summary, you are transforming the matrix in to 1 row and matrix multiplying it by a column of boolean values generated by the logical test. The addition is implicit in the matrix multiply

Accedi per commentare.

Più risposte (2)

x=randi(100,20,1);
sx = sum(x(x<=55))

2 Commenti

This didn't work. Should I mention that the list of numbers are in matrix form?
sx = sum(x(x(:) <= 55));

Accedi per commentare.

Alternatives:
sum(x .* (x <= 55))
or
sum(x(find(x<=55)))

8 Commenti

This is something I tried but it didn't work either. It gives an error for the whole thing and doesn't really explain why.
What error did it give, and for which version?
The first one should have worked on an array, but would have given column sums. The second one should just plain have worked.
Question 1: is your matrix perhaps symbolic values? You cannot compare symbolic values to a number.
Question 2: is your matrix perhaps an odd data type such as a MATLAB object? Or is it perhaps int64() and you do not have a R2011 MATLAB ?
I don't have the Mind Reading Toolbox. What's your error and can you give some sample code to generate your array that will illustrate your error? Also, do you want what your title says (equal to a certain value) or what the subject body says (less than a certain value)? Please clarify since you're being ambiguous.
Walter:
??? Index exceeds matrix dimensions.
Error in ==> Homework at 12
sum(x(find(x<=55)))
>>
Question 1: No symbols, just a matrix with a few hundred integers.
Question 2: I am on MATLAB 7.11.0 R2010
Image Analyst:
??? Index exceeds matrix dimensions.
Error in ==> Homework at 12
sum(x(find(x<=55)))
>>
Less than 55. Not sure how to generate the array in a helpful way, it is a random assortment of a few hundred numbers that was previously given. Any sequence of numbers should do. They are all positive whole numbers.
That doesn't make sense. I ran this and it worked just fine:
x = randi(100,20,20);
s1=sum(x(x < 55))
s2=sum(x(find(x < 55)))
Both s1 and s2 gave the same value (meaning you don't need the find() function). I got no error whatsoever. Run the code I gave and tell me if that works fine. If it does, then try to see what is different about your code. Make sure you didn't use x one time and X another time - MATLAB is case sensitive.
Your code works just fine. The previous code was less than or equal to instead of just less than. I can only assume that interfered with it somehow, even though it should have given a value. But that's the only difference between the two.
No, that's not it. It works just fine with <=. I just changed it to less than because that's what you said. But <= will also work fine - just try it. There must have been some other reason that you're not telling us and we don't know because you didn't share your code.
@Walter: What about logical indexing? FIND is not useful here:
sum(x(x<=55))

Accedi per commentare.

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by