Average If function

Average If function - Messages

#1 Posted: 12/28/2023 3:55:01 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: User

Hi all, there seems to be no function similar to the Excel function "AverageIf" in SMath. Let me set up a hypothetical example with two vectors:

z = [1 2 3 4 5]
M = [50 50 25 20 30]

Let's say that I want to calculate the average value of M for z => 3 AND z <=5. In this case, it should be (25+20+30)/3=25. What would be the best way to go about this? The "Mean" function in SMath returns the mean of the whole vector, so when I write a For loop with an If condition to extract values of M for the interval z, I don't know how to assign "blank" values to the cells that fall outside the defined range so it doesn't affect the average if it makes sense. What I then get is (0+0+25+20+30)/5=15

Thanks in advance as always!
#2 Posted: 12/28/2023 7:13:05 AM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Worksheet7.png
Worksheet7.sm (4 KiB) downloaded 23 time(s).

M*c / c*c would also do.
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 users liked this post
NDTM Amarasekera 12/28/2023 5:38:00 PM
#3 Posted: 12/28/2023 11:58:26 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: User

Aha, that should work.

Thanks!
#4 Posted: 12/28/2023 4:04:11 PM
overlord

overlord

547 likes in 1330 posts.

Group: Moderator

This is another way.

Regards

AverageIf.sm (5 KiB) downloaded 28 time(s).

2023-12-28_11-03.png
1 users liked this post
NDTM Amarasekera 12/28/2023 5:38:00 PM
#5 Posted: 12/29/2023 5:55:40 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: User

Took me a couple of minutes to wrap my head around this one, but now it makes sense.

Thanks!!

Wrote

This is another way.

Regards

AverageIf.sm (5 KiB) downloaded 28 time(s).

2023-12-28_11-03.png



#6 Posted: 12/29/2023 6:19:06 AM
overlord

overlord

547 likes in 1330 posts.

Group: Moderator

This maybe an easier way.
Regards

2023-12-29_01-18.png
1 users liked this post
NDTM Amarasekera 12/29/2023 9:03:00 AM
  • New Posts New Posts
  • No New Posts No New Posts