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

Click For Summary
SUMMARY

The discussion focuses on defining a matrix M in terms of another predefined matrix N using Mathematica. The original approach using a For loop was ineffective, as it only set the diagonal elements of M. A more efficient method is to utilize built-in functions such as DiagonalMatrix, which directly creates a diagonal matrix from the first row of N. Alternative methods include using Array and SparseArray functions for matrix construction.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of matrix operations in Mathematica
  • Knowledge of built-in functions like DiagonalMatrix and Array
  • Basic programming concepts, particularly loops and conditionals
NEXT STEPS
  • Learn how to use DiagonalMatrix in Mathematica for efficient matrix creation
  • Explore the Array function for custom matrix generation in Mathematica
  • Investigate SparseArray for memory-efficient matrix representations
  • Review the Mathematica documentation on matrix operations and constructions
USEFUL FOR

This discussion is beneficial for Mathematica users, especially those involved in mathematical modeling, data analysis, and anyone looking to optimize matrix operations in their code.

AxiomOfChoice
Messages
531
Reaction score
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
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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K