Message Boards Message Boards

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

[?] Generate table for any odd number?

Posted 6 years ago

Hi, i need to generate a Table for any k odd. I really don't know how to explain it, so i put 2 examples of what i need. Its like making a pyramid by eliminating ones in each row.

k = 5;
   {{1, 1, 1, 1, 1}, {0, 1, 1, 1, 0}, {0, 0, 1, 0, 0}}
k=7;
    {{1, 1, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 1, 0, 
       0}, {0, 0, 0, 1, 0, 0, 0}} 

I tried arrays and other stuff but i couldnt do it. Thanks.

2 Replies

Hi,

something like this?

generateArray[k_?OddQ] := Table[ArrayPad[ConstantArray[1, m], {(k - m)/2, (k - m)/2}, 0], {m, 1, k, 2}]
generateArray[_] := "Invalid input!"

This generates:

generateArray[5]

(* {{0, 0, 1, 0, 0}, {0, 1, 1, 1, 0}, {1, 1, 1, 1, 1}}*)

and

generateArray[11]
(*{{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 
  0}, {0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 1, 1, 1, 
  0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1}}*)

For non-odd inputs it says:

generateArray[6]
(*"Invalid input!"*)

You can also plot this:

ArrayPlot[generateArray[101]]

enter image description here

A better way of doing it might be to use CellularAutomaton. Your particular problem obviously corresponds to the automaton with the rule 254 (see Stephen Wolfram's book).

RulePlot[CellularAutomaton[254]]

enter image description here

g[k_?OddQ] := CellularAutomaton[254, {{1}, 0}, Evaluate[(k - 1)/2]]
g[_] := "Invalid input"

Should also do it.

g[5]
(*{{0, 0, 1, 0, 0}, {0, 1, 1, 1, 0}, {1, 1, 1, 1, 1}}*)

and

g[7]
(*{{0, 0, 0, 1, 0, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1}}*)

and

g[4]
(*"Invalid Input"*)

Hope this helps.

Cheers,

Marco

PS: Sorry the previous answer only showed up after I submitted mine....

POSTED BY: Marco Thiel

Hi Jaime,

The function ConstantArray can be combined with Table to generate the lists of 1s. Starting with, for example k = 7, and iterating down to 1 in steps of (-2):

Table[ConstantArray[1, i], {i, 7, 1, -2}]

...evaluates to...

{{1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1}, {1}}

Now we can wrap that ConstantArray function with CenterArray, using ConstantArray[1, i] as the first argument, and k = 7 as the second argument. This pads each of the lists of 1s with 0s at either side, in order to make lists of length k = 7:

Table[CenterArray[ConstantArray[1, i], 7], {i, 7, 1, -2}]

...evaluates to...

{{1, 1, 1, 1, 1, 1, 1}, {0, 1, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 1, 0, 
  0}, {0, 0, 0, 1, 0, 0, 0}}

The last step now is to generalize this by defining a function pyramidArray that takes any odd integer k, and creates the required lists:

pyramidArray[k_?OddQ] := 
 Table[CenterArray[ConstantArray[1, i], k], {i, k, 1, -2}]

For example

pyramidArray[5]

...evaluates to...

{{1, 1, 1, 1, 1}, {0, 1, 1, 1, 0}, {0, 0, 1, 0, 0}}

I hope this helps,

Regards,

Rob

POSTED BY: Robert Ferguson
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