Skip to main content
1-Visitor
September 20, 2013
Question

Simple problem

  • September 20, 2013
  • 4 replies
  • 4697 views

Basicaly what im trying to do is :

Untitled.png

So im trying to make a matrix which has 1 on the main diagonal and on the place right to it, while having -1 on the other side.

Im losing my mind cause i cant make it work and i dont know why, help please

4 replies

19-Tanzanite
September 21, 2013

Something like this?

matrica.PNG

Alan

1-Visitor
September 21, 2013

Or like this?

matrix.png

消息编辑者为:盗版 用户

25-Diamond I
September 21, 2013

From your screenshot and the description it seems to me that the solution of Pirates is not what you are searching for but that Alan had created what you are after, right?

So for whatever its worth her are two more possible solutions. The first one won't work for n=1, but I guess that should be no problem. Personally I'd prefer the first one (just one loop, no if's)

matric_2.png

1-Visitor
September 21, 2013

your answer is right,brief and not concern with the value of ORIGN

for my poor english, i made misunderstanding

25-Diamond I
September 21, 2013

No problem 😉

Here is a solution without any loops, but I still would prefer my first method.

matric_3.png

25-Diamond I
September 21, 2013

Im losing my mind cause i cant make it work and i dont know why, help please

Keep your mind, please, you may need it 😉

There are already some solutions for your task in this thread, so I'd like to try to explain here why your program failed. I assume you got -1 as a result and no matrix, no matter what you provided as parameter.

You probably know that MC would begin to count rows and columns of matrices beginning with 0. This can be changed using the system variable ORIGIN. As you just provided a picture I can only guess that you have ORIGIN set to 1, otherwise it would not make much sense to begin the loops counting with 1.

Your routine places 1's in the diagonal and then a 1 to the right and a -1 below each diagonal element. One problem ist that you do this for the bottom right element, too. That way you create an extra column to the right and an extra row at the bottom.

Solution is to let your routine work for the first n-1 diagonal elements only and then set the 1 at the bottom right in an extra step.

There is no need to set m[i,j to zero as Mathcad will fill unassigned elements with zero automatically - it does no harm doing it, just an unnecessary line of code. Also I have combined your 3 identical if's into one.

If a program does not use an explicit return statement it returns the result of the last line executed - in case of your program this is -1. So you have to add a last line and put the desired result value (m) there. Personally I prefer to use explicitely the return statement but its not absolutely necessary here to do so.

matric_4.png

Find in the attached file also an ORIGIN-aware version of that routine.

EDIT: Attached the last version of this document