Basicaly what im trying to do is :
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
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)
your answer is right,brief and not concern with the value of ORIGN
for my poor english, i made misunderstanding
No problem 😉
Here is a solution without any loops, but I still would prefer my first method.
deal with the identity(n).
linear algebra and matrix is difficult - -
i need some time to digest your solution
is it necenssary to define the function's result when n=1 or give an user's error msg?
i need some time to digest your solution
Fell free to ask.
is it necenssary to define the function's result when n=1 or give an user's error msg?
That depends on the way the function would be used. It throws an error for negative values, non-integer values of n or if n is a matrix or a string. It would be possible to catch all those possibilities and either return a dummy value (1) or a meaningful errormessage. But the question is if that would be worth the effort.
The solutions presented so far all have their limitations concerning the value of n and all require n to be an integer scalar:
Alan's solution: n>=3
matric_1: n>=2
matric_2: n>=1
matric_3: n>=2
matric_4 (= Sandro's original algorithm): n>=2
I don't know what Sandro would need this routine for, but I guess it will be no problem if it does not work for n=1 or even not for n=2. All routines can be made to work for n=1, too, using a simple "if n=1 ... otherwise" construct.
Find attached a version of matric_3 which is two lines shorter and also an explanation of the way the matrix is constructed.
I found another solution by submatrix() without loops,
In the attachment ,the example shows the steps.
消息编辑者为:盗版 用户
btw:it can be short in 3 lines by del 1st line and replace M with identity(n+1) in 2nd and 3rd line.
盗版 用户 wrote:
I found another solution by submatrix() without loops,
Nice idea.
You can even make it more compact
or a bit less messy
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.
Find in the attached file also an ORIGIN-aware version of that routine.
EDIT: Attached the last version of this document