Average If function

Average If function - 消息

#1 已发布: 2023/12/28 03:55:01
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: 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 已发布: 2023/12/28 07:13:05
Martin Kraska

Martin Kraska

1,362个赞,共2,276个帖子。

组: Moderator

Worksheet7.png
Worksheet7.sm (4.2 KiB)已下载 1567 次。

M*c / c*c would also do.
Technische Mechanik mit SMath Studio: https://link.springer.com/book/10.1007/978-3-658-50592-9
1位用户喜欢此帖子
NDTM Amarasekera 2023/12/28 17:38:00
#3 已发布: 2023/12/28 11:58:26
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: User

Aha, that should work.

Thanks!
#4 已发布: 2023/12/28 16:04:11
overlord

overlord

600个赞,共1,381个帖子。

组: Moderator

This is another way.

Regards

AverageIf.sm (5.16 KiB)已下载 1609 次。

2023-12-28_11-03.png
1位用户喜欢此帖子
NDTM Amarasekera 2023/12/28 17:38:00
#5 已发布: 2023/12/29 05:55:40
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: 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.16 KiB)已下载 1609 次。

2023-12-28_11-03.png



#6 已发布: 2023/12/29 06:19:06
overlord

overlord

600个赞,共1,381个帖子。

组: Moderator

This maybe an easier way.
Regards

2023-12-29_01-18.png
1位用户喜欢此帖子
NDTM Amarasekera 2023/12/29 09:03:00
  • 有新帖
  • 无新帖