Matlab shorthand for `for` with nested `if` (like Python's list comprehension)

北慕城南 提交于 2019-12-13 03:32:35

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/9993189/matlab-shorthand-for-for-with-nested-if-like-pythons-list-comprehension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!