2012-04-03 98 views
0

This question以某種方式解決問題,但不是從我正在尋找的一面。嵌套`if`的``for`的Matlab簡寫(如Python的列表理解)

我想映射一個數組到另一個,只挑選低於某個閾值的元素。基本上是一個for循環,其中一個if條件語句檢查閾值。

我知道arrayfun函數,但我不知道一種方法來將條件語句放入它沒有定義一個新的函數。

有沒有辦法用內聯指令執行這個任務?

回答

4

也許這是你在找什麼:

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. 
+2

或者:A(arrayfun(@(x)x <0.5,A)),我認爲這是可以更容易地擴展到矩陣。 – Yuri 2012-04-03 12:38:15

+0

是的arrayfun +匿名函數。更一般。 – 2012-04-03 12:39:33

+0

(same edit;)) – 2012-04-03 12:40:45