Message Boards Message Boards

0
|
2036 Views
|
1 Reply
|
4 Total Likes
View groups...
Share
Share this post:

Using ;; with Part[ ]?

Posted 2 years ago

Hello. I found a code in the Wolfram language on the Internet and I am trying to figure it out. Please help me understand the purpose of each command digit on the second line. I tried to look at Part and Span in the help, but I still didn't get it. Thanks.

data = Import["dest\\Test.xlsx", "Data"];
x = data[[1, 2 ;; - 2, 2 ;; - 2]];
POSTED BY: Sofia Knyazeva

Sofia,

The data in the file evidently has nested data that comes in as a list.

let's make something up:

data = {{a1, {bxx, byy, bzz, buu, bvv}, {cxx, cyy, czz, cuu, 
    cvv}, d1} , b, c}

data[[1]] gives the first element in the list (ignoring the b and c elements (which also could have been lists)

{a1, {bxx, byy, bzz, buu, bvv}, {cxx, cyy, czz, cuu, cvv}, d1}

next

data[[1,2;;-2]]

gives the second item and all items up to the second to last item in the result above:

{{bxx, byy, bzz, buu, bvv}, {cxx, cyy, czz, cuu, cvv}}

and once again they take the second item and all items up to the second to last item:

I made a confusing typo in my first edit of this post --

When you have a span, the next part range applies to each of the elements in the span:

In[4]:= data[[1, 2 ;; -2, 2 ;; -2]]

Out[4]= {{byy, bzz, buu}, {cyy, czz, cuu}}

This is useful for matrix operations. For example, if you want to take one column of a matrix you can use data[[1,All]] to get every item in the matrix

I hope this helps.

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract