3
$\begingroup$

How do I override a calculated value within a matrix with an assigned value? Here is an example of what I mean. Consider matrix Y,

Y = MatrixForm[Table[i^j,{i, 0, 1, 0.5}, {j, 0, 2, 1}]]

with the output

enter image description here

Of course, the Indeterminate in Y[1, 1]is due to 0^0. How do I override the Indeterminate output with say Y[1, 1] = 1 while creating the matrix? Thank you.

$\endgroup$
4
  • 6
    $\begingroup$ A few things, 1) MatrixForm is a wrapper, you probably want MatrixForm[ Y = Table[ .... ] ] 2) Parts are taken and assigned with Part which has an alias in [[ ]] so what in a more classic C-derived language would be Y[1, 1] in Mathematica is Y[[1, 1]]. So all together you want Y = Table[ ... ]; Y[[1,1]]=1; MatrixForm[Y]. $\endgroup$
    – b3m2a1
    Commented Sep 11, 2017 at 4:36
  • $\begingroup$ If[i==j==0,1,i^j] $\endgroup$ Commented Sep 11, 2017 at 4:50
  • $\begingroup$ @b3m2a1 Thank you for the tweak. Your suggestion works pretty fine. $\endgroup$
    – D. Andrew
    Commented Sep 11, 2017 at 4:51
  • $\begingroup$ Related, perhaps duplicate: (10977), (60575), (64179), (102007) $\endgroup$
    – Mr.Wizard
    Commented Sep 11, 2017 at 12:36

3 Answers 3

6
$\begingroup$

In general, if you don't know in advance where Indeterminate entries are going to appear, you can use /. (ReplaceAll) to replace them with whatever you like. So

Y = Table[i^j, {i, 0, 1, 0.5}, {j, 0, 2, 1}];
(* {{Indeterminate, 0., 0.}, {1., 0.5, 0.25}, {1., 1., 1.}} *)

Then

Y /. {Indeterminate -> 1} 
(* {{1, 0., 0.}, {1., 0.5, 0.25}, {1., 1., 1.}} *)
$\endgroup$
5
$\begingroup$

FWIW, there is some undocumented functionality for generating Vandermonde matrices:

LinearAlgebra`VandermondeMatrix[{0, 0.5, 1}, Transpose -> True]
   {{1., 0., 0.}, {1., 0.5, 0.25}, {1., 1., 1.}}

Edit 7/19/2022

In the current version, the following should be evaluated instead:

StructuredArray`VandermondeMatrix[{0, 0.5, 1}, List]
$\endgroup$
4
$\begingroup$

Also

Block[{Indeterminate = 1}, Quiet@Table[i^j, {i, 0, 1, 0.5}, {j, 0, 2, 1}]] 
   // MatrixForm // TeXForm

$\left( \begin{array}{ccc} 1 & 0. & 0. \\ 1. & 0.5 & 0.25 \\ 1. & 1. & 1. \\ \end{array} \right)$

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