Matlab的嵌套`if'的简写(如Python的列表理解) [英] Matlab shorthand for `for` with nested `if` (like Python's list comprehension)

查看:604
本文介绍了Matlab的嵌套`if'的简写(如Python的列表理解)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不知何故解决了这个问题,但不是从我想找的一方。



我想将一个数组映射到另一个数组中,阈。对于循环来说,基本上是一个,用 if 条件语句来检查阈值。

我知道 arrayfun 函数,但我不知道如何在没有定义新函数的情况下将条件语句放入其中。 / p>

有没有一种方法可以通过内联指令来执行这个任务?

解决方案

<

  A =(0:49)./ 50; %生成初始数组。 
B = A(A <0.5); %将一个数组映射到另一个数组中,仅挑选低于某个阈值的元素。


This question somehow addresses the problem, but not from the side I'm looking for.

I'd like to map an array into another, picking only the elements below a certain threshold. Basically a for loop, with an if conditional statement which checks the threshold.

I'm aware of the arrayfun function, but I don't know a way to put the conditional statement in it without defining a new function.

Is there a way to perform this task with an inline instruction?

解决方案

Maybe this is what you are looking for:

 A = (0:49) ./ 50; % Generate the initial array.
 B = A( A < 0.5 ); % Map an array into another, picking only the elements below a certain threshold.

这篇关于Matlab的嵌套`if'的简写(如Python的列表理解)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆