3
$\begingroup$

I have a 2-level list in the form of a $m\times n$ matrix: {{a11,a12,a13,...,a1n},{a21,a22,a23,...,a2n}, ..., {am1,am2,am3,...,amn}}, in which the numbers are normally decimals. Now, I need to compare each element in the $(n+1)$-th level to the corresponding element in the $n$-th level (i.e., the previous level). If the element in the $(n+1)$-th level is larger than that of the $n$-th level, it gives a $+$ sign; if the absolute of the difference between the two numbers is smaller than a small value, say, $0.001$, it gives a $=$ sign; otherwise a $-$ sign. The comparison should be carried out up to the last sub-list to generate a "$+/-/=$" sign table in a $(m-1)\times n$ matrix from finally.

For example, given a list of lists:

{{1.1, 2.3 ,3.4, 4.3, 5.01},{2.1, 1.2, 2.5, 5.6, 5.001},{1.2, 1.201, 1.2, 6.3, 3.7}}

Supposing a function will do the job. After the above manipulation, the desired function should give the following $2\times 5$ matrix {{+,-,-,+,=}, {-,=,-,+,-}}.

Can somebody please help me to do this with MMA. Thank you in advance.

$\endgroup$

1 Answer 1

6
$\begingroup$

You can use Differences + Clip

 Clip[Differences[lst], {-.01, .01}, {"-", "+"}] /. _?NumericQ -> "="

{{"+", "-", "-", "+", "="}, {"-", "=", "-", "+", "-"}}

Alternatively, Differences + Chop + Sign

rule = {1 -> "+", -1 -> "-", 0 -> "="};

Sign[Chop[Differences[lst], .01]] /. rule

{{"+", "-", "-", "+", "="}, {"-", "=", "-", "+", "-"}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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