Skip to main content
1-Visitor
September 9, 2014
Solved

Subtract every value in a vector from another vector

  • September 9, 2014
  • 3 replies
  • 4723 views

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.

Best answer by RichardJ

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.

3 replies

12-Amethyst
September 9, 2014

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

19-Tanzanite
September 9, 2014

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?

1-Visitor
September 10, 2014

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

RichardJ19-TanzaniteAnswer
19-Tanzanite
September 10, 2014

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.

25-Diamond I
September 9, 2014

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

1-Visitor
September 10, 2014

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.

25-Diamond I
September 10, 2014

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.