Skip to main content
1-Visitor
Sep 9, 2014 10:21 PM UTC
Solved

Subtract every value in a vector from another vector

I am decent with mathcad for simple calculations but lack understanding of the programming. I am really wanting to get a bit better but don't have any teaching and/or references. I thought at least this discussion board could be a great place for me to get help and tips.

For this problem I have 2 vectors each containing 100 values. What i am looking to do is subtract each and every single value in each vector from eachother. I think I may need to use some sort of looping routine in order to accomplish this but I don't know how. Maybe there is another way that is better. Sheet is attached with comments in red. Any help would be greatly appreciated. Also if anyone could point me in a good direction (books, etc) to become more familiar with creating these types of routines (for loops, nested for loops, etc). That would be greatly appreciated as well.

RichardJAnswer
19-Tanzanite
Sep 10, 2014 1:42 PM UTC

Here you go. It's just a simple double loop. I could calculate the index for the result vector, Diff, from i and j, but the way I did it using a third variable, k, is easier.

If you go to "help", "tutorials", "features in depth", there is a programming tutorial.

8 replies

12-Amethyst
Sep 9, 2014 10:29 PM UTC

Based on my understanding of your question, it looks like you have already accomplished that which you ask. What am I missing?

Translate
19-Tanzanite
Sep 9, 2014 10:51 PM UTC

It's not clear what you want. Do you want to subtract every value in Xb from the first value in Xa, then every value in Xb from the second value in Xa, etc?

Translate
1-Visitor
Sep 10, 2014 1:06 PM UTC

Richard, this is exactly what I wish to do. Sorry for being unclear and thanks for the reply

Translate
RichardJ19-TanzaniteAnswer
19-Tanzanite
Sep 10, 2014 1:42 PM UTC

Here you go. It's just a simple double loop. I could calculate the index for the result vector, Diff, from i and j, but the way I did it using a third variable, k, is easier.

If you go to "help", "tutorials", "features in depth", there is a programming tutorial.

Translate
Werner_E
25-Diamond I
Sep 9, 2014 10:59 PM UTC

I am confused, too, as to what you want to achieve. To create a vector consisting of the difference of the corresponding values in two vetcor, you simply have to subtract those vectors. No need for programming loops, not even range varaibles, as you did.

It looks like you have done in the sheet exactly what you demanded, so whats the problem? Is it that the minimum of XDIFF is shown as 0 ft while it should be 0.397 ft? This is because you did not consider that Mathcad numbers vector elements beginning from 0 by default. Your range variables are running beginning with 1, so your vectors have an additional element number 0 with the value 0.

You may set the system variable ORIGIN to 1 it you really want to do it the way you did or leave it at the default 0 and change your range varibles and some calculations to fit the new numbering. I prefer leaving ORIGIN at 0 - see atchment.

1.png

Translate
1-Visitor
Sep 10, 2014 1:09 PM UTC

Werner, what my original question was is exactly what Richard described above. However, thank you for your reply pointing out the zero value due to ORIGIN := 1. I did not know this was the case and will need to be careful in the future with this. I will try to stick wtih your suggestion and always leave this value set to zero.

Translate
Werner_E
25-Diamond I
Sep 10, 2014 2:55 PM UTC

r m wrote:

Werner, what my original question was is exactly what Richard described above. However, thank you for your reply pointing out the zero value due to ORIGIN := 1. I did not know this was the case and will need to be careful in the future with this. I will try to stick wtih your suggestion and always leave this value set to zero.

I see.

Find attached some additional ways to do what you want. Some still using ranges, others using matrices instead of vectors. Chose whatever is appropriate.

Translate