23
$\begingroup$

I have a problem about the padding list about Partition command. For instance,

Partition[{a, b, c, d, e, f, g}, 3, 1, {-2, 1}, {x, y, z}]
{{z, a, b}, {a, b, c}, {b, c, d}, {c, d, e}, {d, e, f}, {e, f, g}, {f,g, y}, {g, y, z}}

However, my understanding about padding list shown as below:

enter image description here

Another example

Partition[{a, b, c, d, e, f}, 3, 3, {1, 1}]
 {{a, b, c}, {d, e, f}}

My understanding:

enter image description here

Question

Can someone give me a good explanation?

$\endgroup$
4
  • 1
    $\begingroup$ I tend to agree with you about what the expected output should be. I don't have an explanation unfortunately but look forward to reading one if someone can explain it. $\endgroup$ – Mike Honeychurch Jan 29 '15 at 6:39
  • $\begingroup$ Related: (64925) (your own question) $\endgroup$ – Mr.Wizard Jan 29 '15 at 7:35
  • 1
    $\begingroup$ I just want to add I really like the way you ask your questions! $\endgroup$ – Kuba Jan 29 '15 at 12:52
  • 1
    $\begingroup$ @Kuba,Thanks, I am very happy to hear that:) I found gradually that the understanding of construct is very important for learnnig Mathematica. $\endgroup$ – xyz Jan 29 '15 at 13:03
17
+500
$\begingroup$

Example #1

Let me make your example a bit smaller for brevity:

Partition[{a, b, c, d}, 3, 1, {-2, 1}, {x, y, z}]
{{z, a, b}, {a, b, c}, {b, c, d}, {c, d, y}, {d, y, z}}

This is in effect:

PadRight[{a, b, c, d}, 7, {x, y, z}, 1]

Partition[%, 3, 1]
{z, a, b, c, d, y, z}

{{z, a, b}, {a, b, c}, {b, c, d}, {c, d, y}, {d, y, z}}

Think instead:

enter image description here +
enter image description here Equals
enter image description here

Example #2

The documentation states:

enter image description here

Critical to your example is: "appear at or after position kR in the last sublist."

Therefore in this case it does not matter if kR is any of: 1, 2, 3, -1, -2, -3, because each will be satisfied by:

{{a, b, c}, {d, e, f}}

Partition does not violate the cyclic order of the list. For the output to be as you anticipated:

{{a, b, c}, {d, e, f}, {f, a, b}}

The element f would have to be taken out of sequence; the next element should be a.

$\endgroup$
6
  • $\begingroup$ +1, Good explanation, thanks:), Could you give me a explanation about another example,See update of my question. $\endgroup$ – xyz Jan 29 '15 at 7:08
  • $\begingroup$ Ok,@Mr.Wizard, Today, I found that the understanding of the advanced usage of Partition is the basis of understanding the built-in function(like ListCorrelate, ListConvolve etc). $\endgroup$ – xyz Jan 29 '15 at 7:17
  • $\begingroup$ thanks. thinking in terms of how PadRight works explains it. $\endgroup$ – Mike Honeychurch Jan 29 '15 at 7:25
  • $\begingroup$ @ShutaoTang Please see my update and tell me if it is understandable. $\endgroup$ – Mr.Wizard Jan 29 '15 at 7:33
  • $\begingroup$ OK,@Mr.Wizard, I got it:) $\endgroup$ – xyz Jan 29 '15 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.