Matlab shorthand for `for` with nested `if` (like

2019-09-02 17:27发布

问题:

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.