3
$\begingroup$

Assume the following code

generatedMsg = {1, 0, 0, 0, 1, 1, 0};
transmittedMsg = {1, 1, 0, 0, 1, 0, 0};

Now, I want to print out something along these lines:

$$ \begin{align*} \text{Generated message:}{}&\; 10001100\\ \text{Transmitted message:}{}&\; 1\color{red}{1}001\color{red}000 \end{align*} $$

How should I go about doing it?


Excuse the $\LaTeX$, but I do not know how to color text by other means.

$\endgroup$
  • 2
    $\begingroup$ In general you may be interested in this topic $\endgroup$ – Kuba Apr 14 '15 at 20:04
4
$\begingroup$
generatedMsg = {1, 0, 0, 0, 1, 1, 0};
transmittedMsg = {1, 1, 0, 0, 1, 0, 0};

diff = Position[Unitize[generatedMsg - transmittedMsg], 1];

colored = MapAt[Style[#, Red] &, transmittedMsg, diff];

Grid[{
  Prepend[generatedMsg, "Generated message:"],
  Prepend[colored, "Transmitted message:"]
  }, Alignment -> Right]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks for both Unitize and MapAt! I should look at documentation more carefully. $\endgroup$ – d125q Apr 14 '15 at 20:13
2
$\begingroup$

For variety:

sF := Style[#, {Black, {Red, Bold}}[[1 + Abs@Subtract@##]]] & @@@ Transpose @ {##} &

Column[Row /@ {##, sF @ ##}] &[transmittedMsg, generatedMsg]

enter image description here

Alternatively,

sF2 := Style[#, {Black, {Red, Bold}}[[1+ #2]]]& @@Transpose@{# , Unitize@Subtract@##} &
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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