How would you prepend a column to a grid? I have seen Appending and Prepending columns to a dataset in but that is applied to datasets and I want to retain things as a grid. I need to prepend an "offset" column to a data set and so far haven't seen any easy way of doing this that retains the grid structure (I've seen a "columns" method but it does not seem to retain the same alignment with the rest of the columns). Could use some help.

Update I'm still having some issues with the solns proposed so let me post the code I am using--maybe I am just asking the wrong question

I'd like to be able to prepend a column that shows the offsets in hex

hexdata = ReadByteArray["..<some file>"];
nbytes = Length@hexdata;
addrs = Table[BaseForm[#, 16] &@x, {x, 0, nbytes/16 // Floor}];

hexshowAmount = nbytes/8;
hex = (hexdata[[1 ;; hexshowAmount]] // Normal);

tbl = hex // Partition[#, UpTo@16] &;
nrows = Length@tbl;
ncols = Length@tbl[[1]];

myFunc[val_] := PrimeQ[val]; (* Must Return True or False *)

labels = Flatten@
      myFunc[tbl[[i]][[j]]], {i, j} -> Hue[RandomReal[], .2, .9], 
      Nothing], {i, nrows}, {j, ncols}] // Quiet;

(* original method *)
outputhex = 
  tbl // Grid[Map[BaseForm[#, 16] &, #, {2}], Frame -> All, 
     Alignment -> Center, Spacings -> Center, 
     Background -> {None, None, labels}, ItemSize -> {2, 2}] &;

(* current method *)
Multicolumn[BaseForm[#, 16] & /@ hex, 16, 
 Appearance -> "Horizontal", Frame -> All, Alignment -> Center, 
 Spacings -> Center, Background -> {None, None, labels}, 
 ItemSize -> {2, 2}]

Which results in

enter image description here

I guess another issue is that I have since found using Multicolumn to suit my purpose better. Is there a way to prepend Multicolumn that is easier?


Ahh feel like a dummy

Join[{Range[5]}, Transpose@#] &@%[[1]] // Transpose // Grid

However this will mess up the highlighting so need to change the line

labels = Flatten@
      myFunc[tbl[[i]][[j]]], {i, j } -> Hue[RandomReal[], .2, .9], 
      Nothing], {i, nrows}, {j, ncols}] // Quiet;


labels = Flatten@
      myFunc[tbl[[i]][[j]]], {i, j+1 } -> Hue[RandomReal[], .2, .9], 
      Nothing], {i, nrows}, {j, ncols}] // Quiet;

Hoping there is a less hacky way to accomplish this tho.

  • 1
    $\begingroup$ Add a sample grid, simple is fine, as long as it reproduces your actual problem. $\endgroup$
    – MarcoB
    Dec 10, 2020 at 14:03

1 Answer 1

array = Array[a, {4, 5}];

grid = Grid @ array

enter image description here

newcolumn = Array[b, 4];

newarray = Transpose[Prepend[newcolumn]@Transpose[array]];

Grid @ newarray

enter image description here

Alternatively, you can use MapThread or Join:

newarray2 = MapThread[Prepend, {array, newcolumn}];

newarray3 = Join[List /@ newcolumn, array, 2];

newarray == newarray2 == newarray3

Update: If you need to use grid and newcolumn as inputs, you do

newgrid = Grid @ Transpose[Prepend[newcolumn] @ Transpose[First @ grid]]

enter image description here

  • $\begingroup$ I'm having some difficulty getting this to work. I'm thinking it's because you're joining arrays and then converting to a grid? $\endgroup$
    – skyfire
    Dec 10, 2020 at 15:20
  • $\begingroup$ @skyfire, please see the update. $\endgroup$
    – kglr
    Dec 10, 2020 at 15:27
  • $\begingroup$ Have been getting an error with concerns over incompatible dimensions. I updated the question with my code so hopefully this helps illustrate my issue better. $\endgroup$
    – skyfire
    Dec 10, 2020 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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