cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

sum elements of a vector using if statement

Otilia
6-Contributor

sum elements of a vector using if statement

Hello,

 

I have the vector from the attached file with the input data.

I need to do both the sum and the total number of all positive elements from N22 (only positive). How can i do this?

 

Thank you,

Otilia

 

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:Otilia)

For example like this:

LM_20190226_SumUp.png

View solution in original post

6 REPLIES 6
LucMeekes
23-Emerald III
(To:Otilia)

For example like this:

LM_20190226_SumUp.png

Otilia
6-Contributor
(To:LucMeekes)

Thank you very much LucMeekes. It works.

 

Otilia

Werner_E
24-Ruby V
(To:Otilia)

For whatever it may be worth here is a slightly shorter approach

B.PNG

Otilia
6-Contributor
(To:Werner_E)

Hello,

 

I managed to get the results I wanted for a larger matrix (see attached) with the help from both of you and it is greatly appreciated.

 

What i still don't understand is the functions you used for sum and count. For count you used the 'vector sum' function, for sum it's some sort of product. No wonder with the vector sum i couldn't get anywhere when i started this exercise. Why?

 

Thank you,

Otilia

 

 

LucMeekes
23-Emerald III
(To:Otilia)

The result of (V>0) is a vector with the same size as V, where all elements are either 0 or 1. That is: 1 when the corresponding element in V is larger than 0, and 0 when it isn't.
The vector sum of (V>0) is the sum of all 1's, that's the count of all your positive elements in vector V.
If you multiply two vectors, like V*(V>0), the result is the total sum of all multiplications of all corresponding elements. So all positive elements of V get multiplied by 1, the other elements get multiplied by 0. The total sum of all those products is then the sum of all positive elements in V.

Success!
Luc
Otilia
6-Contributor
(To:LucMeekes)

It makes sense now.

 

Thank you.

Top Tags