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

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Compare columns in 2 matrices

RW_10992920
4-Participant

Compare columns in 2 matrices

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.

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:RW_10992920)

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?

Werner_E_1-1718919259027.png

Werner_E_2-1718919808574.png

 

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.

Werner_E_0-1718926921712.png

 

Prime 9 file attached

View solution in original post

8 REPLIES 8
LucMeekes
23-Emerald III
(To:RW_10992920)

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:

LucMeekes_0-1718656673482.pngLucMeekes_1-1718656680333.png

Then we get this from match:

LucMeekes_2-1718656713756.png

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:

LucMeekes_3-1718657270142.png

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:

LucMeekes_4-1718657366884.png

If we call that function with the two matrices A and B, we find:

LucMeekes_5-1718657405639.png

Now match can return multiple values, if they exist. If we change A like this:

LucMeekes_6-1718658117044.png

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:

LucMeekes_7-1718658226662.png

and call it from a find function:

LucMeekes_8-1718658264613.png

And if I now call that with (the modified matrix) A and B, I get:

LucMeekes_9-1718658316748.png

There's a more powerful implementation of the function, that does not use recursion:

LucMeekes_0-1718661772658.png

LucMeekes_2-1718661841907.png

LucMeekes_3-1718661849353.png

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:

Capture.JPG

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():

LucMeekes_1-1718744612624.png

See attached Prime 4 file, works in express.

 

Success!
Luc

Werner_E
25-Diamond I
(To:RW_10992920)

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.

Werner_E_0-1718756837524.png

 

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.

Werner_E
25-Diamond I
(To:RW_10992920)

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?

Werner_E_1-1718919259027.png

Werner_E_2-1718919808574.png

 

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.

Werner_E_0-1718926921712.png

 

Prime 9 file attached

Announcements

Top Tags