Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
Hi
I need to subtract two different matrix . one has ( 7 rows, 2 column) and the other has (5 rows , 1 column)
I know the other own is shorter so the result should be the same number in the bigger matrix. like it treat the missing numbers as zeros
Is that possible ?
Thank you
The sheet is attached
Solved! Go to Solution.
So q is not a 1x2 matrix of vectors as in your example but rather a 2 x 1 matrix (or at the end an n x 1 matrix.
Adding units fortunately doesn't do any harm as Prime in this case seems to apply the correct unit even if we just add a dimensionless zero:
I further assume that, while q is a nested n x 1 vector holding column vectors the N always is just a simple column vector. That means that none of the involved matrices will ever consist of more than 1 column. Is this correct?
Additionally I assume that you wan the result being of the same structure as Q (nested vector of vectors), right?
It's always best to be clear about what the task is and what you want the result to be, so there's no room for misunderstandings.
If my assumptions are correct, the following could be a sensible solution to your problem:
I also added shorter variants which seem to fit just your special situation
Prime 11 sheet attached
Hello ,
Thank you for you reply. The thing is I need to write that as a program ( so it calculate itself without the need to change it manually ) , because in my original sheet ( p values ) can exceed 100 values and also for N
I can't manual add zeros to N every single time I use this calculation.
I appreciate your reply , though
Yusra
Simply change my subscript 6 for length(x)-1
@YA_10963798 wrote:
Hi
I need to subtract two different matrix . one has ( 7 rows, 2 column) and the other has (5 rows , 1 column)
No! the second in your sheet has 6 rows, not 5.
And the first is not a 7x2 matrix but rather a nested 1x2 matrix whose elements are both 7x1 matrices. Guess you should have used "augment" as shown by Alan.
One way to deal with your problem is to use a function which adds the necessary zero elements if necessary. If the matrix provided as argument is larger than demanded its returned unchanged:
This function now can be used in a "subtract" function. I placed "blowUp" into "subtract" as local function so "subtract" would be self-contained.
Another way could be to use nested for loops to do the job:
Prime 11 sheet attached
Or could it be that you intend to subtract the column vector N from EACH column in matrix p???
So which of the two results below did you expect?
Or maybe it wasn't an accident that you defined p as a nested vector?? After all you didn't comment on Alan's modification (augment).
Maybe you had something like this in mind??
Hi,
An expansion of Alan's solution that works without manual input based on size of p.
Your appraoch returns a wrong result if N is a matrix with equal dimensions as p (7x2 matrix in this example) and it fails if N has more rows than p.
Yusra had not explicitly clearly specified if N always is a 1-column matrix and if the rows of N can't exceed the rows of p.
Hi Werner.
Firstly sorry for the late reply. I left office before you answered , I haven't got the chance to follow up.
Actually it wasn't an incident that I wrote p as a nested vector the real calculations look like this :
I need to subtract NSF values from each matrix in q .
I will try the solution you submitted . Thanks a lot
Yusra
So q is not a 1x2 matrix of vectors as in your example but rather a 2 x 1 matrix (or at the end an n x 1 matrix.
Adding units fortunately doesn't do any harm as Prime in this case seems to apply the correct unit even if we just add a dimensionless zero:
I further assume that, while q is a nested n x 1 vector holding column vectors the N always is just a simple column vector. That means that none of the involved matrices will ever consist of more than 1 column. Is this correct?
Additionally I assume that you wan the result being of the same structure as Q (nested vector of vectors), right?
It's always best to be clear about what the task is and what you want the result to be, so there's no room for misunderstandings.
If my assumptions are correct, the following could be a sensible solution to your problem:
I also added shorter variants which seem to fit just your special situation
Prime 11 sheet attached
Hi, Yusra,
There are a few programming languages that do what you want, effectively making arrays conformable for some purpose. This involves "stretching" or "shrinking" one or more arrays so that some operation can be performed on them that would otherwise raise an error. There are also rules that determine whether it is meaningful to make arrays conformable.
A further nicety is that the "default" value differs across purposes. For example, addition uses zero, multiplication uses one, string concatenation uses the empty string, and array operations use the empty array. https://en.wikipedia.org/wiki/Conformable_matrix
In some languages, this altering of array sizes to make them conformable is called broadcasting, eg in Python's NumPy. https://numpy.org/devdocs/user/basics.broadcasting.html
Werner's got you covered as far as a working solution goes, but it uses programming. Some of us only have Mathcad Express, which does not support programming, so we have to find other ways to do things.
Appropriately, as it's nearing Halloween, I've cobbled together some functions I had lying around to create a Frankensheet in Mathcad Express 10 that seems to solve your problem. It's a bit of a hack, so best viewed as experimental.
Unfortunately, the lack of programming in Mathcad Express means that Express functions can appear somewhat baroque and overly complicated. Additionally, many useful Mathcad functions are classified as "Premium", meaning they are disabled in Express, requiring workarounds to overcome their absence. That is not a criticism of Express, BTW, it's just a fact that more capability costs money, and Express is pretty capable in its own right.
Definitions of the broadcasting functions conform and Conform.
Solution to your problem (an 11-vector subtracted from a nested vector of 12-vectors)
Different forms of array conformation.
Definitions of the fill, value repetition, and element repetition functions.
Any other needed functions are defined in a collapsed Area at the top of the worksheet. There are far more than are strictly needed for Sub, but I'm afraid I don't have the time to sift out the unnecessary ones (or get rid of all of the "scribbling" and "notes" I wrote along the way).
Stuart
Hi Stuart,
It is great to hear from you.
What an amazing answer, that will solve all my future problems as well, Thankkkkkkkkkkkkkkkkkkk youuuuuuuu
Thank you for those kind words, Yusra.
And don't go thinking your future problems are solved. There are always new ones lurking in the background, ready to pounce when least wanted! 😊
I meant to add a couple of other functions to the worksheet that might meet some of your other needs, that is, converting between nested vectors and matrices. In the example below, I've also used Ta to better show (IMO) a lengthy nested vector than showing the nested vectors as row vectors. As you an see, the functions are unit aware (as is Sub from the previous worksheet).
(Mathcad Express 10 worksheet attached)
Stuart
PS. I've also included a few new functions to fill rows or columns with values from a vector, as well as a couple that swap rows or columns. They might prove useful to somebody.
Thank you very much Stuart. I think I need a strong coffee to learn this. I appreciate your help . Have a nice day
Hi Yusra,
You needn't spend too much (if any) time on the fill functions; it's mainly the mat2nst and nst2mat that might prove useful to you. I'd probably rewrite them as true Mathcad programs, both to improve performance and conciseness and to reduce dependencies on functions that are Mathcad Express workarounds for missing Mathcad Prime functionality.
Enjoy your coffee!
Stuart
