cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

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

bsatola
4-Participant

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.

 

bsatola_0-1647112496777.png

 

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

3 REPLIES 3

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

Thank you! Very useful.

 
LucMeekes
23-Emerald III
(To:bsatola)

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

 

Top Tags