Matrix elements not correctly defined by programming (expecting 1 for diagonal and 0 otherwise)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Matrix elements not correctly defined by programming (expecting 1 for diagonal and 0 otherwise)
Trying to create a matrix by programming, where the diagonal should be 1 and 0 otherwise. I even get the same result if I replace if-else to define every element 1. This does not make any sense to me. I assume there is an easy solution, but I was unable to find a solution by searching.
Solved! Go to Solution.
- Labels:
-
Algebra_Geometry
-
Programming
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi,
Try this:-
You can use programming as you have tried or the identity() inbuilt function.
Mathcad arrays start at index 0 unless you set ORIGIN=1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi,
Try this:-
You can use programming as you have tried or the identity() inbuilt function.
Mathcad arrays start at index 0 unless you set ORIGIN=1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you! Very useful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
In this specific case, where you only want array elements other than 0 on the diagonal, your program can be much simpler:
This makes use of the fact that all array elements are assigned the value 0 by default. The variable ORIGIN is used to make the array creation independent of the actual value of ORIGIN.
But of course, as Terry pointed out, for an identity matrix, you should better use the built-in identity function:
Success!
Luc
