Message Boards Message Boards

0
|
2968 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Compute the sum of all entries along an super- or sub-anti-diagonal?

Posted 7 years ago

I'm a beginner. So bear with me please if my questions look dumb. I'm trying to define a function which takes a matrix as input and returns the sum of all entries along a specified sub or super anti-diagonal.

Here is a piece of code that I found at stackexchange, which returns the k-th anti-diagonal.

antidiag[m_, k_] :=  Diagonal[m[[k ;; -1, k ;; -1]][[-1 ;; 1 ;; -1,    1 ;; -1 ;; 1]]]

Here are my questions:

  1. I understand the general use of ;; for Span. But I don't understand the many -1 here. What do k;;-1 and -1 ;; 1 ;; -1 mean, respectively?
  2. For a matrix m, I understand the indexing using m[[...]]. But what does m[[...]][[...]] mean? Can anyone give me a pointer for this syntax?

Thanks a lot!!

POSTED BY: K X
2 Replies

negative indices count from the end: -1 the last, -2 the penultimate, et cetera so:

{4,5,6,7,8,9}[[-1;;1;;-1]] 

does reverse: "pick from end to beginning in steps of -1"

POSTED BY: Sander Huisman
Posted 7 years ago

Aha! Thanks, I see it now! Thanks very much!

POSTED BY: K X
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