# How to update all values of a matrix with a multiple of their existing values

I have a matrix, and I want to multiply all the values in each element by Sqrt[2/3]. Here is the code.

    m = Table[
If[(n)^2 + (j)^4 + (k)^3 + (l) == 0, {n, j, k, l}, Nothing], {n,
0, 10, 1}, {j, 0, 10, 1}, {k, -10, 0, 1}, {l, -10, 0, 1}] //
Flatten[#, 3] & // Select[Length@# > 0 &]; m // MatrixForm


So the columns of the matrix correspond to the values of (n,j,k,l). Now I want to create a new matrix, where I introduce a counter value that starts at say 0.01 and increments by 0.01 with each iteration, call it a++.

The new 3 column matrix will take the value of (n,j,a) and put it into the matrix as the first row, and also (while keeping the same value of a) will also add (k,l,a) as the second row and then move the next row of the original matrix and so the same process, with the counter a incremented.

This code, with the help of people here, is:

    Join @@ MapIndexed[Partition[#1, 2] /. x_?VectorQ :> Join[x, 0.01 #2] &, m]
ListPlot3D[%, PlotStyle -> PointSize[0.02]]


However, before plotting this, I want to also update each element of the value of a to the value of a times 1/Sqrt.

The previous post is here: Create a new matrix from an existing matrix and then plot it

Any help would be appreciated.

• So, ListPlot3D[% / Sqrt, PlotStyle -> PointSize[0.02]]? – march Jan 31 '20 at 5:54

Using an alternative method suggested in this answer to generate the two matrices:

ClearAll[l, n, k, j]
m2 = Values @ Solve[{Total[{l, n, k, j}^Range] ==
0, -10 <= l <= 0 && -10 <= k <= 0 && 0 <= n <= 10 &&
0 <= j <= 10}, {n, j, k, l}, Integers];

m2 == m


True

new2 = ArrayReshape[MapIndexed[Riffle[#, .01 #2[], {3, -1, 3}] &, m2],
{2 Length@m2, 3}];


You can get the desired result by using .01 /Sqrt instead of .01 as the increment:

new2a = ArrayReshape[MapIndexed[Riffle[#, .01 #2[] / Sqrt, {3, -1, 3}] &, m2],
{2 Length@m2, 3}];


Alternatively, you can scale the matrix new2 by multiplying each row with {1,1, 1/Sqrt} or using ScalingTransform[{1,1,1/Sqrt}] on new2:

new2b = {1, 1, 1/Sqrt} # & /@ new2;
new2c = ScalingTransform[{1, 1, 1/Sqrt}]@new2;

new2a == new2b == new2c


True

ListPlot3D[new2a] just multiply your matrix by 1/Sqrt

aa = RandomInteger[5, {2, 2}];


$$\left( \begin{array}{cc} 1 & 5 \\ 1 & 0 \\ \end{array} \right)$$

1/Sqrt aa // MatrixForm • that is okay for the first part - but I do not know how to multipy the new matrix represented by this code Join @@ MapIndexed[Partition[#1, 2] /. x_?VectorQ :> Join[x, 0.01 #2] &, m] ListPlot3D[%, PlotStyle -> PointSize[0.02]] – Betty Jan 31 '20 at 1:44