I have a very simple problem, with me misunderstanding the use of Module
s and Function
s in general, in Mathematica.
I have a function of which I want to input a matrix, and in the function I have a need of knowing the number of columns to make operations upon these. To this I use Dimensions[]
, which works fine while using a matrix directly, but seems to fail whenever I try to input the formula to a function and/or Module
.
The following lines works when using a matrix directly, yet if I want to save the output directly, I can't seem to make it work:
OrtMat[X_] = Module[{m = Dimensions[X][[2]]},
For[i = 1, i < m + 1, i++,
If[i > 1,
For[j = 1, j < i, j++,
X[[All, i]] =
X[[All, i]] -
X[[All, i]].X[[All, j]]/Norm[X[[All, j]]]*X[[All, j]];
X[[All, i]] = X[[All, i]]/Norm[X[[All, i]]];
],
X[[All, i]] = X[[All, i]]/Norm[X[[All, i]]]
];
]
]
which very simply orthogonalizes the matrix. As said the only error popping up is with dimensions, it comes with the output:
Part::partw: Part 2 of {} does not exist. >>
Orthogonalize[]
andEigensystem[]
? $\endgroup$