Message Boards Message Boards

Ways to access elements of a list or an array

GROUPS:

Dear All,

For your information only.

A code to access elements of a list or an array.

Grid[{
  {"Description\n\nUsage with:\nmatx={{2,3,5},{7,11,13},{17,19,23}},\n\
b={6,1,8,-4}", "Mathematica function", "Expression", "Output"}
  , {"Select an element", " - ", "b[{3]]\nmatx[[2,3]]", "8\n13"}
  , {"Select a row", " - ", "matx[[2]]", "{7,11,13}"}
  , {"Select a column", " - ", "matx[[All,1]]", "{2,7,17}"}
  , {"Select a submatrix", " - ", "matx[[2;;3,1;;2]]", 
   "{{7,11},{17,19}"}
  , {"Select the first element", "First[list]", "First[b]", "6"}
  , {"Select the last element", "Last[list]", "Last[b]", "-4"}
  , {"Select the first row", "First[mat]", "First[matx]", "{2,3,5}"}
  , {"Select the last row", "Last[mat]", "Last[matx]", "{17,19,23}"}
  , {"Take the first n elements of a list", "Take[list,n]", 
   "Take[b,2]", "{6,1}"}
  , {"Take the last n elements of a list", "Take[list,-n]", 
   "Take[b,-2]", "{8,-4}"}
  , {"Take the nth to kth elements of a list", "Take[list,{n,k}]", 
   "Take[b,{2,4]", "{1,8,-4}"}
  }
 , Alignment -> Left
 , Frame -> {{Red}, {Red}}
 , Background -> {{Lighter[Yellow, .9]}, {Lighter[Yellow, .9]}, None}
 , Spacings -> {3, 2}
 , Dividers -> All
 , ItemStyle -> Directive[FontSize -> 16, Bold]
 , FrameStyle -> Thick]
Print[Style[
  "mat = array of mXm elements,m > 1\nlist = list of m elements\nn, k \
= integer", 18, Red, Bold]]

enter image description here

Cheers,.....Jos

POSTED BY: Jos Klaps
Answer
15 days ago

Thanks for sharing the overview. Other ways of accessing elements would be Extract, and a special case Diagonal. Last elements can also be obtained by Part using -1 as the index. Then there is also the special case: the index = 0 to get the head.

POSTED BY: Sander Huisman
Answer
14 days ago

Hi Sander,

Thanks for your reply and your information.

I was not aware about the functions of 'Extract" and "Diagonal". Thanks for these tips !

Best Regards,....Jos

POSTED BY: Jos Klaps
Answer
13 days ago

You must correct an error in the second row - third column. It must be $b[[3]]$ instead of $b[{3]]$.

POSTED BY: Valeriu Ungureanu
Answer
12 days ago

Hi Valetiu,

Thanks to let me know. Sorry for the typo.

Best Regards,....Jos

POSTED BY: Jos Klaps
Answer
12 days ago

Group Abstract Group Abstract