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$
  • $\begingroup$ Post the code you have, don't be shy $\endgroup$ – Dr. belisarius Mar 16 '16 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$ – Dr. belisarius Mar 16 '16 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 '16 at 3:19
  • $\begingroup$ post code, note an image so folks can cut paste $\endgroup$ – george2079 Mar 16 '16 at 4:12
  • $\begingroup$ just posted it at the end $\endgroup$ – john g Mar 16 '16 at 4:17
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$
  • $\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 '16 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 '16 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 '16 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 '16 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$
  • 1
    $\begingroup$ :-D You were more verbose when you answered him before. $\endgroup$ – Jason B. Mar 16 '16 at 10:47
  • 1
    $\begingroup$ Why not just 100` (values/table - 1) // TableForm? $\endgroup$ – Mr.Wizard Mar 16 '16 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$

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.