Mathematica Asked on June 21, 2021
How would you prepend a column to a grid? I have seen Appending and Prepending columns to a dataset in 10.0.2.0
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@
Table[If[
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
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?
UPDATE2
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@
Table[If[
myFunc[tbl[[i]][[j]]], {i, j } -> Hue[RandomReal[], .2, .9],
Nothing], {i, nrows}, {j, ncols}] // Quiet;
into
labels = Flatten@
Table[If[
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.
array = Array[a, {4, 5}];
grid = Grid @ array
newcolumn = Array[b, 4];
newarray = Transpose[Prepend[newcolumn]@Transpose[array]];
Grid @ newarray
Alternatively, you can use MapThread
or Join
:
newarray2 = MapThread[Prepend, {array, newcolumn}];
newarray3 = Join[List /@ newcolumn, array, 2];
newarray == newarray2 == newarray3
True
Update: If you need to use grid
and newcolumn
as inputs, you do
newgrid = Grid @ Transpose[Prepend[newcolumn] @ Transpose[First @ grid]]
Correct answer by kglr on June 21, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP