[英]Conditional statement in matlab function argument
I am wondering if its possible to have conditional statements in a function argument. 我想知道是否有可能在函数参数中包含条件语句。 for ex, testarray = [1,5,8,5,7,23,61,16] 例如,testarray = [1,5,8,5,7,23,61,16]
psum = sum(testarray>2 & testarray<10) psum = sum(testarray> 2&testarray <10)
will it possible to implement something like this in matlab. 是否有可能在matlab中实现这样的事情。
I would really appreciate an example. 我真的很喜欢一个例子。
Yes, please see the example below using your data. 是的,请使用您的数据查看以下示例。
testarray = [1,5,8,5,7,23,61,16]; % your array
Find sum of all numbers greater than 2 and less than 10 in testarray 在测试数组中查找大于2且小于10的所有数字的总和
psum = sum(testarray(testarray>2 & testarray<10));
The idea is that you find the indices of the numbers that meet the condition (ie, testarray>2 & testarray<10 in this case), extract the numbers by indexing into testarray, and then sum them. 这个想法是找到符合条件的数字的索引(在这种情况下,即testarray> 2&testarray <10),通过索引到testarray中提取数字,然后求和。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.