Skip to main content
6-Contributor
October 14, 2022
Solved

Conditional sum of values in one vector based on another vector

  • October 14, 2022
  • 1 reply
  • 1208 views

Hi, wondering if someone can help me out with this one please?

 

Mathcad Prime 7

 

I have a vector [A], which could have any number of rows, each element can be either zero or one, and the origin element is always one.

 

I have another vector [B], which has the same number of rows as [A], and contains data (forces).

 

I would like to create another vector [C], where each element in [C] should equal the corresponding element in [B] if the corresponding element in [A] equals one. If an element in [A] equals zero then that value in [B] gets added to the last element in [C] that related to a one in [A]. That continues until another one appears in [A], and it starts again. Elements in [C] that correspond with zero elements in [A] should equal zero.

 

For example:

Gaz_Ball_0-1665734732899.png

Thanks

Best answer by Werner_E

I guess this function should do the trick

Werner_E_3-1665743432733.png

P6 file attached

 

 

1 reply

Werner_E25-Diamond IAnswer
25-Diamond I
October 14, 2022

I guess this function should do the trick

Werner_E_3-1665743432733.png

P6 file attached

 

 

Gaz_Ball6-ContributorAuthor
6-Contributor
October 14, 2022

Fantastic! Thank you.