1
$\begingroup$

If i have a 22 by 25 table of values which depend on n and r, and a list of values that depends on r that is a 22 by 1 matrix, how can I find the percent difference between each element, by row in the matrix.

For instances, finding the percent difference for each element in the top row of the 22 by 25 matrix with respect to the first value from the 22 element vector.

here is the code:

ClearAll["Global`*"]
ω = 50;
c = 299792458;
λ = 400;
t1 := (2*Pi*r)/(c + ω*r);
t2 := (2*Pi*r)/(c - ω *r);
t21 = t2/t1;
δtc = N[Table[t2 - t1, {r, 1, 22, 1}], 10];
invδtc = Reverse[N[Table[t2 - t1, {r, 1, 22, 1}], 10]];
A := (n r^2*Sin[(2*Pi)/n])/2;
δtp = N[Table[(4*ω*A)/c^2, {r, 1, 22}, {n, 3, 26}],10]; 
diff = N[Table[((δtc - δtp)/δtc)*100, {r, 1, 22}, {n, 3,26}], 10];

Here is my results, as u can see every row has the same answer, which obviously does not make sense:

enter image description here

$\endgroup$
5
  • $\begingroup$ Post the code you have, don't be shy $\endgroup$ Mar 16, 2016 at 2:59
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Mar 16, 2016 at 2:59
  • $\begingroup$ You probably want to use Set Delayed (i..e, :=) instead of Set (=) when defining t1, t2, and A. $\endgroup$
    – bill s
    Mar 16, 2016 at 3:19
  • $\begingroup$ post code, note an image so folks can cut paste $\endgroup$
    – george2079
    Mar 16, 2016 at 4:12
  • $\begingroup$ just posted it at the end $\endgroup$
    – john g
    Mar 16, 2016 at 4:17

4 Answers 4

2
$\begingroup$

Perhaps this toy example will help. Note in the following:

  • / acts component-wis
  • I have only shown fractional change, just multiply matrix by 100
  • MatrixForm is just a display format. In the image of code it is being used for operations

    m1 = RandomInteger[{1, 10}, {10, 10}];
    m2 = RandomInteger[10, {10, 10}];
    d = m1 - m2;
    p = d/m1;
    Grid[{{"m1", "m2", "m1-m2", "percent change"}, 
    MatrixForm /@ {m1, m2, d, p}}]
    

enter image description here

$\endgroup$
4
  • $\begingroup$ that definitely helps, but since my tables are not of the same dimensions, it does not seem to be working $\endgroup$
    – john g
    Mar 16, 2016 at 3:47
  • $\begingroup$ @johng I apologize for missing different dimensions. As I read your question more carefully you may able to achieve result using e.g.Map[f,m,{2}] where f subtracts is #-e/#& (or 100 x), where e is the element and it isbeing applied at level 2. :) $\endgroup$
    – ubpdqn
    Mar 16, 2016 at 3:53
  • $\begingroup$ No need for the apology, and i can try that, i have no idea what Map or the second recommended command is $\endgroup$
    – john g
    Mar 16, 2016 at 3:56
  • $\begingroup$ @johng think of # as place holders. So (#-e)/#& (what I wrote was wrong) is just a more compact way of writingFunction[x,(x-e)/x] or given e, $f(x)=(x-e)/x$. Look at documentation re: levels. But as a start Map[f,m,{2}] where m={{a,b},{c,d}} yields {{f[a],f[b]},{f[c],f[d]}}. Good luck. Playing with small examples helps (me at least). :) $\endgroup$
    – ubpdqn
    Mar 16, 2016 at 4:03
1
$\begingroup$

Demonstrating with a 3 x 4 matrix.

table = {
   {1, 2, 4, 8},
   {1, 2, 4, 8},
   {1, 2, 4, 8}};
values = {2, 4, 6};

MapThread[N[100 (#1/#2 - 1)] &, {values, table}];

TableForm[%]
100.     0.       -50.    -75.
300.     100.     0.      -50.
500.     200.     50.     -25.

Checking last item, percentage difference between 8 and 6 is -25%.

$\endgroup$
2
  • 1
    $\begingroup$ :-D You were more verbose when you answered him before. $\endgroup$
    – Jason B.
    Mar 16, 2016 at 10:47
  • 1
    $\begingroup$ Why not just 100` (values/table - 1) // TableForm? $\endgroup$
    – Mr.Wizard
    Mar 16, 2016 at 10:56
0
$\begingroup$
arr = Array[Subscript[a, ##] &, {3, 5}];
subtractRow = First@Array[Subscript[b, ##] &, {1, 5}];

Maybe?

((arr [[#]] - subtractRow)/arr[[#]]) & /@ Range[First@Dimensions@arr] // MatrixForm

matrix form of result

Or

Function[{arr, row}, ((arr [[#]] - row)/arr[[#]]) & /@ 
    Range[First@Dimensions@arr]][arr, subtractRow] // MatrixForm
$\endgroup$
0
$\begingroup$

I am using Mathematica 12.0 and I was able to get the percent difference from two matrices simply through "normal" operations. For example:

r1 = {{1, 3, 5, 4}, {5, 4, 5, 4}, {4, 3, 1, 1}, {4, 3, 4, 5}}

rstarf2 = {{1.2482, 2.44921, 4.89503, 4.35648}, {4.89717, 4.1552, 4.61887, 
4.37045}, {4.24131, 2.48646, 1.02565, 1.19075}, {3.78053, 3.55694, 
4.49958, 4.18843}}

rstarf2 - r1 =
{{0.248201, -0.550792, -0.104967, 0.356484}, {-0.102826, 
0.155204, -0.381132, 0.370454}, {0.241307, -0.513542, 0.0256549, 
0.190748}, {-0.219468, 0.55694, 0.499581, -0.811568}}

diff2pct = (rstarf2 - r1)/r1 =
{{0.248201, -0.183597, -0.0209934, 0.0891209}, {-0.0205651, 
0.038801, -0.0762264, 0.0926134}, {0.0603266, -0.171181, 0.0256549, 
0.190748}, {-0.054867, 0.185647, 0.124895, -0.162314}}

Verified that the elements in the final matrix above are actually % differences.

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