Defining one matrix in terms of another in Mathematica with FOR loop

In summary, the conversation discusses the issue of defining a matrix M in terms of a predefined matrix N. The individual is attempting to use a for loop to set the diagonal elements of M to be the first row of N, but is encountering difficulties. Others suggest using built-in matrix methods and provide alternative ways to create the desired matrix. The conversation also includes tips on efficient matrix construction and references to further resources.
  • #1
AxiomOfChoice
533
1
THIS ISN'T WORKING AT ALL!

I'm trying to define a matrix M in terms of a predefined matrix N by using the following for loop:

For[a=1,a<=12,a++,M[[a,a]]=N[[1,a]]]

So I just want the diagonal of M to be the first row of N. But this is not working at ALL. Does anyone see what I'm doing wrong?
 
Physics news on Phys.org
  • #2
First of all, http://reference.wolfram.com/mathematica/ref/N.html" , N[Pi]=3.1415...
If you want to use a symbol that looks like N, the easiest is \[CapitalNu] that can be entered using <esc>N<esc>.

Second, you need to set the off diagonal elements of M, not just the diagonal.

Finally, it's more efficient to use built in matrix/array methods than using For loops.

Here's a 12*12 random integer matrix and a 12*12 zero matrix:

Code:
Mat1 = RandomInteger[{-10, 10}, {12, 12}];
Mat2 = ConstantArray[0, {12, 12}];

To implement a loop like the one that you wanted, try

Code:
Do[Mat2[[a, a]] = Mat1[[1, a]], {a, 1, Length@Mat2}]

Ouput the matrix and check that it's correct:

Code:
Mat2 // MatrixForm
Diagonal[Mat2] == First[Mat1]

(* Returns (for the particular random matrix that I got):
-1	0	0	0	0	0	0	0	0	0	0	0
0	9	0	0	0	0	0	0	0	0	0	0
0	0	-4	0	0	0	0	0	0	0	0	0
0	0	0	-2	0	0	0	0	0	0	0	0
0	0	0	0	1	0	0	0	0	0	0	0
0	0	0	0	0	2	0	0	0	0	0	0
0	0	0	0	0	0	2	0	0	0	0	0
0	0	0	0	0	0	0	9	0	0	0	0
0	0	0	0	0	0	0	0	9	0	0	0
0	0	0	0	0	0	0	0	0	-4	0	0
0	0	0	0	0	0	0	0	0	0	3	0
0	0	0	0	0	0	0	0	0	0	0	7

True *)

Probably the best way to create the matrix Mat2 would be to use

Code:
DiagonalMatrix[First@Mat1]

But other options are

Code:
IdentityMatrix[12] First[Mat1]
Array[If[#1 == #2, Mat1[[1, #]], 0] &, {12, 12}]
Array[KroneckerDelta[##] Mat1[[1, #]] &, {12, 12}]
SparseArray[Band[{1, 1}] -> First@Mat1, {12, 12}] // Normal
(* etc... *)

See the Constructing matrices http://reference.wolfram.com/mathematica/guide/ConstructingMatrices.html" for more info.
 
Last edited by a moderator:

Related to Defining one matrix in terms of another in Mathematica with FOR loop

1. How do I define one matrix in terms of another using a FOR loop in Mathematica?

To define one matrix, let's call it matrix A, in terms of another matrix, let's call it matrix B, using a FOR loop in Mathematica, you can use the following syntax:

A = Table[For[i = 1, i <= n, i++, B[[i, i]]], {n, 1, Length[B]}]

2. Can I define multiple matrices in terms of each other using a FOR loop in Mathematica?

Yes, you can define multiple matrices in terms of each other using a nested FOR loop. For example, if you have matrices A, B, and C, you can define A in terms of B and C using the following syntax:

A = Table[For[i = 1, i <= n, i++, B[[i, i]] + C[[i, i]]], {n, 1, Length[B]}]

3. How do I use a conditional statement with a FOR loop to define a matrix in Mathematica?

You can use the If statement within a FOR loop to define a matrix based on a condition. For example, if you want to define a matrix A with values only in the first row of matrix B, you can use the following syntax:

A = Table[For[i = 1, i <= n, i++, If[i == 1, B[[i, i]], 0]], {n, 1, Length[B]}]

4. Can I use a FOR loop to define a matrix with a different increment than 1 in Mathematica?

Yes, you can specify the increment in a FOR loop in Mathematica by adding a third argument after the loop variable and the range. For example, if you want to define a matrix A with values from 1 to 10 with an increment of 2, you can use the following syntax:

A = Table[For[i = 1, i <= 10, i+=2, i], {n, 1, Length[B]}]

5. How do I avoid overwriting values in a matrix when defining it in terms of another matrix using a FOR loop in Mathematica?

You can avoid overwriting values in a matrix by using a temporary variable within the FOR loop. For example, if you want to define matrix A in terms of matrix B without overwriting any values in matrix B, you can use the following syntax:

A = Table[For[i = 1, i <= n, i++, temp = B[[i, i]]; A[[i, i]] = temp], {n, 1, Length[B]}]

Similar threads

Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
322
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
8
Views
178
Back
Top