7
$\begingroup$

This question already has an answer here:

a = {{-800, 1712}, {-801, 1713}, {-806, 1832}}
TableForm[a]
b = a[[All, 2]] - Min[a[[All, 2]]]

How do I make a new matrix (list) whic looks like this

-800  1712 0
-801  1713 1
-806  1832 120

The 3rd column, takes the difference between the value from the 2nd column and it's minimum of the whole 2nd column.

I tried to Join the two, but with no success.

Thanks.

$\endgroup$

marked as duplicate by Yves Klett, Kuba, Sjoerd C. de Vries, rhermans, Dr. belisarius Dec 6 '14 at 13:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

9
$\begingroup$

Perhaps I've missed something, but you seem to just want to append a vector (which you have already calculated) to a matrix.

Documentation for Append[] shows how to do it:

enter image description here

For your case since you already have a and b:

MapThread[Append, {a, b}] // MatrixForm

enter image description here

A different approach enables you to join matrixes to make longer rows:

The following makes a 2 column matrix from your b

b2 = Transpose[{b, b}];
b2 // MatrixForm

enter image description here

The documentation for Join[] shows the way to do what you asked for in your comment:

enter image description here

Note that the 2 at the end of the Join[] operates as a level specification.

For your case:

Join[a, b2, 2];
% // MatrixForm

enter image description here

$\endgroup$
  • $\begingroup$ How would I Append two columns? Do I have to use MapThread[Append, { MapThread[Append, {a, b}], b} ] // TableForm, or there is an easy way? Thanks. $\endgroup$ – Chen Stats Yu Dec 6 '14 at 16:34
  • $\begingroup$ @ChenStatsYu - see addition to the answer. $\endgroup$ – Jagra Dec 6 '14 at 19:01

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