Skip to main content
Tweeted twitter.com/#!/StackMma/status/497915638623768576
added 23 characters in body
Source Link
khanhnguyendata
  • 5.2k
  • 1
  • 23
  • 33

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"x" is replaced by its numbered form, then the next x"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.

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. 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.

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.

Source Link
khanhnguyendata
  • 5.2k
  • 1
  • 23
  • 33

Replace all symbols in a matrix with their numbered forms

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. 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.