Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Please help me, I'm writing my project and dont know how to solve this big matrix:

enter image description here

I have to compute return row by row: 1st row: (18.32-17.14)/17.14 ; (19.17-18.32)/18.32 .... 2nd row:(11.79-13.86)/13.86 ; (11.37-11.79)/11.79 ....

share|improve this question

closed as off-topic by Oleksandr R., m_goldberg, Yves Klett, Kuba, István Zachar Dec 10 '13 at 11:15

  • The question does not concern the technical computing software Mathematica by Wolfram Research. Please see the help center to find out about the topics that can be asked here.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Look up Differences and Most in the help. Also, Listable functions. –  Oleksandr R. Dec 8 '13 at 3:27
    
Thanks a lot! It's really a great help to me. –  MirkoSrb Dec 8 '13 at 3:34
5  
This question appears to be off-topic because it is too localized and probably not useful for future visitors. –  Oleksandr R. Dec 8 '13 at 3:36
    
I didn't know Differences so I found it a useful question. –  Chris Degnen Dec 8 '13 at 17:01

2 Answers 2

up vote 2 down vote accepted
m = {{17.14, 18.32, 19.17, 20.63},
     {13.86, 11.79, 11.37, 13.45},
     {49.9, 48.77, 52.3, 56.87}};

compute = Partition[#, 2, 1] /. {a_, b_} :> b/a - 1 &;

compute /@ m

{{0.0688448, 0.0463974, 0.0761607}, {-0.149351, -0.0356234, 0.182938}, {-0.0226453, 0.0723806, 0.0873805}}

Alternatively:

m = {{a, b, c}, {d, e, f}};

compute = (Ratios@#) - 1 &;

compute /@ m

{{-1 + b/a, -1 + c/b}, {-1 + e/d, -1 + f/e}}

Edit

Some timings :-

Clear[o, p, q, r]
Subsets[{o, p, q, r}, {2}]
m = RandomReal[20, {3000, 3000}];

f1 = (Differences@#/Most@#) &;
f2 = Partition[#, 2, 1] /. {a_, b_} :> b/a - 1 &;
f3 = (Ratios@#) - 1 &;
f4 = (Rest@#/Most@#) - 1 &;

o = f1 /@ m;
p = f2 /@ m;
q = f3 /@ m;
r = f4 /@ m;
Equal @@@ Subsets[{o, p, q, r}, {2}]
Flatten[Chop[#1 - #2] & @@@ Subsets[{o, p, q, r}, {2}]] // Union

{{o, p}, {o, q}, {o, r}, {p, q}, {p, r}, {q, r}}

{False, False, False, False, True, False}

{-1.16415*10^-10, 0, 1.16415*10^-10}

Only f2 and f4 produce exactly the same results, but the differences are all minute.

t = First@Timing[# /@ m] &;
t /@ {f1, f2, f3, f4}

{0.216709, 14.198865, 0.179863, 0.145772}

The Partition method, (f2), is by far the slowest!

share|improve this answer
m = {{a, b, d}, {d, e, f}}
Differences /@ m /Most /@ m
(*
{{(-a + b)/a, (-b + d)/b}, {(-d + e)/d, (-e + f)/e}}
*)
share|improve this answer

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