2012-02-09 55 views
6

我在Mathematica中有一個列表,並且我試圖獲取列表中的每個其他數字並將其作爲新列表存儲。如何獲取列表中的每個其他項目

目前我有

ReadList["file",Number] 

,其讀出整個列表,{ x1, x2, x3, x4, ... };我只想挑出其他號碼並將其存儲在新的列表中,例如, { x1, x3, x5, ... }

如何做到這一點?

回答

8

嘗試:

yourlist = {a, b, c, d, e, f, g, h}; 
(* use Span: search for Span or ;; in Documentation Center *) 
everyotheritemlist = yourlist[[1 ;; -1 ;; 2]]; 
(* or use Take *) 
Take[yourlist, {1, -1, 2}] 

均可以得到:

{a,c,e,g}  
+2

還是稍短'yourlist [[1 ;; ;; 2]]'。 – 2012-02-09 23:05:10

+0

@Brett的確如此! – kglr 2012-02-09 23:09:59

+0

非常感謝,您是否也碰巧知道如何更改Command ListPlot的步長。它列出了單位步驟,並想知道是否有辦法改變這種情況?再次感謝你的幫助! – user1200775 2012-02-09 23:44:57

1

對於像總有幾十個創造性的方式來做到這一點的數學任務。 kguler已經給你的規範辦法,但這裏的另一個問題:

Partition[yourlist, 2]\[Transpose][[1]] 

(* 
==> {a, c, e, g} 
*) 

順便說一句:有在https://mathematica.stackexchange.com/一個專門的數學Stackexchange網站。 Mathematica社區越來越朝着這個方向發展,所以你可能也想加入我們。

+0

謝謝你的參考! – user1200775 2012-02-09 23:47:04

1

另一種方式:

First /@ ReadList["test.dat", {Number, Number}] 
相關問題