I have 2 questions:

Question 1:

val = Table[10 i + j, {i, 4}, {j, 3}]

{{11, 12, 13}, {21, 22, 23}, {31, 32, 33}, {41, 42, 43}}

enpva = Table[4 x + 3, {x, 2, 4}]

{11, 15, 19}

I create xpol as a matrix of zeros with the same dimensions as val.

Then I replace parts of xpol where for each column of val, if the i-th element, say of val[[All,1]] is equal to the i-th element of enpva, the i-th element of the first column of xpol equals 2; otherwise 1. The command in Matlab is

if val(i, j) == enpva(i), xpol(i, j) = 2; else xpol(i, j) = 1; 

So xpol would be a matrix of 1's and 2's. I then want to take the first differences for each column of xpol (that is, the 2nd-1st element, 3rd-2nd element, 4th-3rd and so on). So xpol at the end would have the one less row but the same number of columns and would have 1's and 0's.

My real version is a larger dimensional matrix for val and enpva, but I just cannot create this matrix xpol. I have tried the command If in Mathematica, but I could not.

Question 2:

I want to make a ListLinePlot for each column of val with legends. I have tried

val1 = 
    ListLinePlot[Table[val[[All, 1]][[i]], {i, 1, 3}], 
      PlotStyle -> Red, 
      AxesLabel -> 
       {"Coal Price", "Value"}],
    "\!\(\*SubscriptBox[\(V\), \(0\)]\)->Red"]


val3 = 
    ListLinePlot[Table[val[[All, 3]][[i]], {i, 1, 3}], 
      PlotStyle -> Red, 
      AxesLabel -> 
        {"Coal Price", "Value"}],
    "\!\(\*SubscriptBox[\(V\), \(0\)]\)->Black"]

Then I tried

label = 
    {Red, Black}, 
    {"\!\(\*SubscriptBox[\(V\), \(0\)]\)", "\!\(\*SubscriptBox[\(V\), \(T\)]\)"}]

and I wanted to combine all of them with

Show [val1, val3, label]

This did not work. Besides, it's a very primitive way of doing this. All I want to do is, do the ListLinePlot of val1 and val3 with colored legends showing the curves val1 and val3.

Any help would be appreciated.

  • 1
    $\begingroup$ Please try to use editing help to make this question tidy. $\endgroup$ – Kuba Feb 11 '17 at 19:11
  • 3
    $\begingroup$ You have asked ten questions on this site so far: each one had to be formatted by a kind soul to make it readable. It is time that you learn how to copy/paste readable code, and how to format your own questions. In addition to the tutorial Kuba mentioned, look at: How to copy code from Mathematica; and How do I format my posts using Markdown or HTML?. It is in your best interest to present a good question: more people will be interested, and you will receive prompter answers. $\endgroup$ – MarcoB Feb 11 '17 at 19:45
  • $\begingroup$ Thank you. I have gone over the post. I will make sure that I format better the next time. $\endgroup$ – Supratim Das Gupta Feb 14 '17 at 16:18

Here is an answer to your list manipulation question.

val = Table[10 i + j, {i, 4}, {j, 3}];
valCol = Transpose[val];
enpva = Table[4 x + 3, {x, 2, 4}];

xpol =
    With[{e = #2}, Map[Function[v, Boole[v == e] + 1], #1]] &,
    {valCol, enpva}]

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

Differences /@ xpol

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

Here is an answer to your plot legending question.

  PlotStyle -> {Red, Blue, Black},
  AxesLabel -> {"Coal Price", "Value"},
  PlotLegends -> SwatchLegend[Automatic, {"val1", "val2", "val3"}]]



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