Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
I'm using Mathcad Prime 9.0.0.0 and I need help comparing the values in 2 matrices. To be more specific, I need a program that will start with the first column and compare the values going row by row, then return the value if there is a match, and if there is not a match then move on to the next column and repeat the comparison process.
In the example matrices, Column 9 Row 4 (-1.63) should be the only value returned.
Solved! Go to Solution.
If I understand correctly, you are just looking for a negative angle theta with T.m(theta) = theta, right?
So Terry obviously had the right instinct.
Why not use one of Primes numerical ways for finding the solution of an equation?
That way you don't have to bother with a matrix j or the function T.s1.
You may also use the symbolics but I won't suggest doing so.
The iteration process you described could be done as shown below. However, I would recommend using Prime's numerical algorithms as shown above. These are faster and offer higher accuracy. The iteration shown below only has an accuracy of 0.01 degree.
Prime 9 file attached
Welcome to this forum. The best first thing you should do is to attach your Prime worksheet file to the message. That saves anyone who tries to help you from retyping the matrix data. It's perfect that you state which version of Prime you're using.
Now to your problem.
I suppose you can guarantee that both matrices have the same number of rows and columns (They're identical in size).
When that is the case you can subtract them from each other and search for 0's. For that you can use the function match(). (Look it up in the online help).
A simple example. If:
Then we get this from match:
Which says that the element at position row=1 and column=1 contains a 0 in the difference matrix A-B. Note that position indexing starts at 0 by default (the to-left element is at position [0,0], the bottom-right one is at [2,2], Such is controlled by the global variable ORIGIN.
So now we have the position of the equal value. We can look it up in either A or B.
Note that match() returns a vector, whose first element is:
a vector, of which the first element is the row index, the second is the column index. So we can write a function to find (the first occurrence of) an equal value in two matrices M1 and M2 like this:
If we call that function with the two matrices A and B, we find:
Now match can return multiple values, if they exist. If we change A like this:
Then match(0,A-B) will find two positions. To deal with that a more complicated function is needed. Because I cannot use Prime's programming, I'll use a recursive function to list the element values:
and call it from a find function:
And if I now call that with (the modified matrix) A and B, I get:
There's a more powerful implementation of the function, that does not use recursion:
And of course it returns the same result:
Oh, you'll get an error message if there are no equal values at the same position in both matrices.
The attached file contains the stuff.
Success!
Luc
Hi,
In just a few steps you could do this:
Enclose your worksheet and we can show you how it works for your sheet.
Cheers
Terry
I think your method of comparison is better, because it will find the exact equals, instead of those values equal to within TOL (as match will do).
Also I found a way to assemble the functionality in a single function, FindsAllEquals():
See attached Prime 4 file, works in express.
Success!
Luc
Here are my 2 Cents worth ...
It was not clear to me which data structure you would need for the result, if you would also need the information about row and column number, etc.
So I decided to create a three column matrix for the result, containing the matching values and their positions.
My approach is not using any lookup/match functions or vectorization - it simply follows the algorithm outlined by you, looping through the matrices row by row.
As the values in your pictures are real values, probably subject to rounding errors and inaccuracies, I guessed it would be better not to check for exact equality. I have chosen a tolerance of 10^-8 but feel free to modify it or replace the expression for exact equals as shown. if you find it more appropriate.
Prime 4 file attached
Hi
It may be possible to use root() or find() solve block to get the solution to the equations. Seeing the worksheet would help.
Cheers
Terry
Not sure if we would need any numeric solver at all. It sure may be possible to give the solution without having to define and compare any matrix.
But without knowing what actually the final goal would be and without seeing the sheet, we can only try to give the questioner what he/she specifically had asked for.
So here is a dumbed down version of the program I wrote. There is a lot to it and I can't post the full thing. But ultimately the point of the program was to start from theta = 0 and increment it by -0.01 until in converges with f1. But I couldn't write a looping program to achieve this and instead went with the method of plugging in a matrix ( j ) of thetas and comparing that to the output matrix. Finding where the numbers converge (or get within a certain percentage) is the answer to to the equation.
Some things to note, SN(X) and CN were programs that would increase the matrix size based on the value of N (i.e. N=1 & N=2 gives 8x8, N=3 gives 12x12, N=4 gives 16x16...) and they had more equations involved, but I removed those for the sake of simplicity.
The FindEqual appears to be the answer I was looking for but I'm looking at the other solutions as well.
If I understand correctly, you are just looking for a negative angle theta with T.m(theta) = theta, right?
So Terry obviously had the right instinct.
Why not use one of Primes numerical ways for finding the solution of an equation?
That way you don't have to bother with a matrix j or the function T.s1.
You may also use the symbolics but I won't suggest doing so.
The iteration process you described could be done as shown below. However, I would recommend using Prime's numerical algorithms as shown above. These are faster and offer higher accuracy. The iteration shown below only has an accuracy of 0.01 degree.
Prime 9 file attached