Skip to Main Content
Mathematica 錦囊妙計
book

Mathematica 錦囊妙計

by Sal Mangano
April 2014
Beginner to intermediate content levelBeginner to intermediate
830 pages
15h 21m
Chinese
GoTop Information, Inc.
Content preview from Mathematica 錦囊妙計
3.9 使用可指定作用層數的指令處理巢狀串列
|
115
使用
Position
指令找尋元素 1,將得到每個元素 1 的位置,輸出位置可使用
Extract
令進行驗證。[Page-115]
若你不想得到所有數值為 1 的元素,此時作用層數便可派上用場。
將作用層數指定為正整數
n
,可由第一層搜尋至第 n 層。
搜尋第1層與第2層所有元素1
若以大括號包覆為
{n}
,則搜尋範圍限制於第
n
層。
僅搜尋第2層的元素1
若作用層數指定為串列
{n,m}
,則搜尋範圍限制於第
n
層至第
m
層之間。
ch03.indd 115 2014/4/2 上午 05:19:12
116
|
第三章
搜尋第3層至第5層所有數值為1的元素
若作用層數指定為負值,則搜尋本身巢狀深度為
n
的元素。[Page-116]
輸出空串列的原因請見後文討論
討論說明
Position
指令搭配作用層數很好判斷,輸出位置串列的長度即代表巢狀深度,因此前文
Position
指令讓你能夠體會作用層數的用法。但你也許會對最後一個範例結果感到驚
訝,你或許會誤以為負作用層數表示由樹狀結構底部開始往上搜尋,因為
Part
指令的
負索引值即是如此作用,但作用層數並無法相提並論,作用層數
-n
表示搜尋本身巢狀
深度為
n
的元素,但所有純量(scalar,如前例中的元素 1)的深度均為 1
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ubuntu for Non-Geeks, 4th Edition

Ubuntu for Non-Geeks, 4th Edition

Rickford Grant, Phil Bull

Publisher Resources

ISBN: 9789863470106