I have a matrix made mostly from "x"
with a few 0
's thrown in:
list = {{0, "x", "x", 0}, {0, "x", "x", "x"}, {0, "x", "x",
"x"}, {"x", "x", "x", "x"}, {"x", "x", "x", "x"}, {"x", "x", "x",
"x"}, {"x", "x", "x", "x"}, {"x", "x", "x", "x"}, {0, "x", "x",
"x"}}
I know that there are 31 "x"
's in the matrix, and I want to replace each of them with a numbered variable: x1
, x2
, and so on til x31
(without quotations). Here's the expected result:
(* {{0, x1, x2, 0}, {0, x3, x4, x5}, {0, x6, x7, x8}, {x9, x10, x11,
x12}, {x13, x14, x15, x16}, {x17, x18, x19, x20}, {x21, x22, x23,
x24}, {x25, x26, x27, x28}, {0, x29, x30, x31}} *)
Below is the list of the numbered variables that you may use:
numberedx = Table["x" <> ToString@i // Symbol, {i, Range@Count[list, "x", {2}]}]
(* {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10,
x11, x12, x13, x14, x15, x16, x17, x18, x19, x20,
x21, x22, x23, x24, x25, x26, x27, x28, x29, x30, x31} *)
I can't for the life of me figure out how to do this. My gut tells me that some procedural method is in order since each "x"
is replaced by its numbered form, then the next "x"
down the line is replaced by the next member in the list of numbered variables. Unfortunately I'm very uncomfortable with procedural programming. I also tried MapIndexed
but the 0
's are mapped as well. I'd really appreciate any help or insight.