Skip to main content
8-Gravel
March 12, 2022
Solved

Matrix elements not correctly defined by programming (expecting 1 for diagonal and 0 otherwise)

  • March 12, 2022
  • 2 replies
  • 1686 views

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.

 

bsatola_0-1647112496777.png

 

Best answer by terryhendicott

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.

 

Capture.JPG

2 replies

21-Topaz II
March 12, 2022

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.

 

Capture.JPG

bsatola8-GravelAuthor
8-Gravel
March 13, 2022

Thank you! Very useful.

 
23-Emerald IV
March 13, 2022

In this specific case, where you only want array elements other than 0 on the diagonal, your program can be much simpler:

LucMeekes_0-1647186428215.png

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:

LucMeekes_1-1647186505823.png

Success!
Luc