Enter formula in matrix forms

Hello I am a new Mathematica user and I have to finish a school project using it. I have the following Matrix Form, I need to calculate the growth rate of the following data by year. The growth rate formula is: (this year's demand - last year's demand)/last year's demand. For example: the growth rate for a 1 room apartment in the year 2010 would be: 0.6%

What would be the easiest and quickest way to add this formula and change all the data in the matrix form automatically? Is there way to do it quickly and efficiently like in Excel or would I have to calculate each number manually and then add it together again into a new matrix form?

Here's the raw data in case you need it:

{{20731, 20695, 20828, 55601, 55703, 55806, 56258, 56754, 57335,
58069}, {66571, 66876, 67230, 87744, 88258, 89099, 90331, 91578,
93166, 94796}, {307262, 308122, 308945, 260490, 261145, 262584,
264070, 265616, 267219, 268669}, {142917, 143811, 144565, 122223,
122928, 124206, 125184, 126318, 127091, 128054}}

• Have you tried anything yet? Commented Jan 7, 2019 at 17:35
• Mathematica isn't like Excel in that you don't add formulas to tables and calculate that way. Try writing some functions and applying them to your data held as a List. In reality, MatrixForm and TableForm are really just display forms, rather than things to be used a la Excel and you'll get much better performance from Mathematica if you use it in the intended way. Commented Jan 7, 2019 at 18:01
• Let data be your data. Then data[[All, 2 ;;]] is all the rows, leaving out the fist column, and data[[All, ;; -2]] is all the rows, leaving out the last column. Conformable matrixes can be elementwise divided as well as subtracted. Does that get you there?
– Alan
Commented Jan 7, 2019 at 18:11
• Next time present your own effort. This will help you get better in Mathematica than just trying to find a site to have other people do straight out your homework. Commented Jan 7, 2019 at 19:37

a = {{20731, 20695, 20828, 55601, 55703, 55806, 56258, 56754,  57335, 58069},
{66571, 66876, 67230, 87744, 88258, 89099, 90331, 91578, 93166, 94796},
{307262, 308122, 308945, 260490, 261145, 262584, 264070, 265616, 267219, 268669},
{142917, 143811, 144565, 122223, 122928, 124206, 125184, 126318, 127091, 128054}
};


Many arithmetic operations automatically act on arrays componentwise, so there is no need for any loop construct, fortunately. In the following, you find several ways how to do it:

A long version using Part ([[]]) and Span (;;):

b = N[a[[All, 2 ;; -1]] - a[[All, 1 ;; -2]]]/a[[All, 1 ;; -2]]


Slightly more compact with Most, Rest, and Map (/@).

b = N[Rest /@ a - Most /@ a]/Most /@ a


A variant using Differences and Most:

b = N[Differences /@ a]/Most /@ a


A variant employing Ratios:

b = Ratios /@ N[a] - 1.


In any case, the returned matrix is the same:

{{-0.00173653, 0.00642667, 1.66953, 0.0018345, 0.00184909, 0.00809949, 0.00881652, 0.0102372, 0.012802}, {0.00458157, 0.00529338, 0.305132, 0.00585795, 0.00952888, 0.0138273, 0.0138048, 0.0173404, 0.0174957}, {0.00279891, 0.00267102, -0.15684, 0.00251449, 0.00551035, 0.00565914, 0.00585451, 0.00603503, 0.00542626}, {0.00625538, 0.00524299, -0.154546, 0.00576815, 0.0103963, 0.00787402, 0.00905867, 0.00611948, 0.00757725}}

For percentages, you simply have to multiply the result by 100

100 b


{{-0.173653, 0.642667, 166.953, 0.18345, 0.184909, 0.809949, 0.881652, 1.02372, 1.2802}, {0.458157, 0.529338, 30.5132, 0.585795, 0.952888, 1.38273, 1.38048, 1.73404, 1.74957}, {0.279891, 0.267102, -15.684, 0.251449, 0.551035, 0.565914, 0.585451, 0.603503, 0.542626}, {0.625538, 0.524299, -15.4546, 0.576815, 1.03963, 0.787402, 0.905867, 0.611948, 0.757725}}

It may turn out helpful for you in the future to look up all these built-in symbols in the documentation.

In addition to engaging the calculation, you may be interested in getting a formatted output table

data= {{20731, 20695, 20828, 55601, 55703, 55806, 56258, 56754,  57335, 58069},
{66571, 66876, 67230, 87744, 88258, 89099, 90331, 91578, 93166, 94796},
{307262, 308122, 308945, 260490, 261145, 262584, 264070, 265616, 267219, 268669},
{142917, 143811, 144565, 122223, 122928, 124206, 125184, 126318, 127091, 128054}
};
growthRate = (data[[All, 2 ;; -1]] - data[[All, 1 ;; -2]])/data[[All, 1 ;; -2]]
TableForm[growthRate]//N


Here is a screenshot of the output