Greetings, im trying to program something that fills a matrix automatically, but you can choose wich rows and columns to make 0 instead.
So far i can do it by hand, just setting what row and colum i want (fil=row), and adding extra conditions to the "if"
I tried to use a matrix instead, like row:=[1 2 3], so if i= any of those 3 values, it assign it 0, but it doesnt seem to work with a matrix, i wrote it something like "if i=fil1_i" (_i meaning subscript), but it doesnt compare it to the value on the matrix.
Im working with Power Flow equations, in this part i need to do a 2x2 matrix each space being a Jacobian matrix. Im trying to automate it, because this is with 2 "bars" so matrix are 2x2, but when its something like 6 bars i dont want to do everything by hand
Solved! Go to Solution.
Thanks, that match function is just what i was looking for. Plus i did something so i can put four 2x2 matrix on a single big matrix (since the jacobian matrix is like that) to adjust the subindex so it starts at 0 when you reach each cuadrant (note that i changed matrix origin to 1, hence those i-1).
Now since you finally have to work with the actual values of that matrix, im looking into, either dont fill those rows/columns from the matrix so it ends up as a 3x3 in this case. Or take the info from this matrix and remove the required rows and columns, so i dont need to make a new matrix and manually put each element looking at the original matrix.
So i applied that same program to calculate the actual values, and like i asked before, how could make a new 3x3 matrix with the non 0 values, so i dont have to do it by hand?, either by adding more commands to the program that gives me this result, or to trim the result matrix
There might be better and more elegant ways, but here is a quick first attempt for a function which trims all rows and columns which consist of all zeros:
Prime 6 worksheet attached
Thanks a lot. I was trying to understand your method, the just do columns but transpose and transpose back is a great idea.
I have a question since i been using mathcad for this semester only, why is it needed to declare the origin when you start a program? i see many people does it, but i notice i can get away with not doing it at all.
Also i didnt knew you could use the input matrix, do some work and output to a different matrix, that way you can fill another matrix with just the info you want. Thats another thing i was trying to figure out.
Very grateful for your help. Plus my teacher was impressed that i took the time to learn to program in mathcad since it wasnt necessary.
You don't HAVE to set the value of ORIGIN. By default it is set to 0. Through the menu you can set it either 0 or 1, but if you define it on the sheet you can set other values as well.
People may tend to define it on (top of) the sheet to make clear which ORIGIN they use.
The double transposition was done out of laziness as I did not want to write a trimZeroRows function, too. But if you are using really huge matrices, this could be a performance drawback. But by huge I am talking of matrices with much more than a few hundreds elements.
The default value for ORIGIN is 0, but I noticed/assumed from your screenshots that you changed that to 1, probably via the menu (Calculation ribbon). An alternate way to set the value of ORIGIN is to simply write ORIGIN:=... at the top of your sheet which also has the benefit that you immediatly see that the default value is changed. Furthermore you could set any value here, not just 0 and 1 but I can't see much usage for setting a different value - I am used to use the default value 0.
So if you set the desired value via the menu, its not necessary to write ORIGIN:=... in your sheet.
When I write utility programs usually I try to make them ORIGIN-aware so they work as expected whatever the value of ORIGIN in the file might be.
So while "for i:=1 .. rows(M)" works OK only if ORIGIN is set to 1, something like "for i:=ORIGIN .. ORIGIN-1+rows(M)" works OK no matter what the value of ORIGIN is. To make it more compact I assign ORIGIN to a variable O at the start of my programs and use O instead of ORIGIN.
BTW few days ago we had a discussion here in the forum about undocumented Prime functions and we came across the functions "update1(..)" and "update2(...)" which also can be used to insert values in an already existing matrix:
The usage of those function may impress your teacher even more 🙂
But I am not sure if they would also work as expected when used with symbolic evaluations - you'll have to give it a try.
Furthermore there is always the potential danger when using undocumented features that they will not be available any more in future versions of the program. Nonetheless its fun playing around with them 😉