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

Highlighted
Moonstone

## returning values in a given "band" from a vector?

To all,

Is there a mathcad command to return all the values within a given band/tolerance?

Example

Assume have a vector fv:=[10,11,12..20]

I want to get a new vector containing all the values (from f) within a given band/tolerance of a given value fi

let 's pick a value within the given vector: fv=15

tolerance = 1.5

vector to be returned : fvband:=[14,15,16]

with tolerance = 4

vector to be returned : fvband:=[11,12,13,14,15,16,17,18,19]

in other words. the new vector should include the values in range: fi-tolerance:fi+tolerance

I can think of a way of iterating but to create 'fvband' but was wandering if there is something built-in!

Thanks

Regards

7 REPLIES 7
Highlighted

## Re: returning values in a given "band" from a vector?

There's no such thing built in, but you can build it yourself. Here's an example.

The function 'Band' returns those values of array A that are within tol from v:

And with a wider tolerance you get:

Success!
Luc

Highlighted

## Re: returning values in a given "band" from a vector?

Which version of Mathcad is this in?

I've been trying to use a vector instead of the 1..10 type argument in a for statement an it dose not work for me.

Thanks

Highlighted

## Re: returning values in a given "band" from a vector?

What Luc has shown should work in every version of Mathcad, even in Prime.

Why don't you tell us which version you are using and show here what you have done so we can help finding the mistake you made.

You write that you used a vector instead of the range 1..10? Luc did not use a range.

Maybe you are using Prime and you did it the other way round, substituting the vector in Lucs solution for a range. This would explain the error.

Werner

Highlighted

## Re: returning values in a given "band" from a vector?

There are some routines which may be of interest to you in the Data Analysis Extension Pack but nothing which will do what you demand out of the box.

Here are a few ways to do it:

The error in the last example is thrown by the Match fucntion when it can't find any match. You may rewrite the Mathc function or throw in an "on error" statement in the Bad1 routine.

Even shorter:

Here the first error is thrown because match can't find any zero and the second error is thrown because trim is told to trim away the whole vector, but there is no "empty bector" in Mathcad.

So here is a routine which considers those cases:

When no matching value within the given range is found, this routine return a 1x1 matrix containing a NaN. You may replace that with anything appropriate to your applicitaion.

Werner

Highlighted

## Re: returning values in a given "band" from a vector?

And here's another way that makes use of a more general filter function, which you can use anywhere you want to pick out just those elements of a vector that meet a simple yes/no test.  filter returns 0 if there are no in-band elements, as 0 corresponds to logical false and hence can be simpler to check in many instances.

Here's an example of how filter could be used ...

Stuart

Highlighted

## Re: returning values in a given "band" from a vector?

 StuartBruff wrote: ... Here's an example of how filter could be used ...

Forgot to add the partial application version of InBand ..

And the direct definition of the tolerance function inside filter

Stuart

Highlighted

## Re: returning values in a given "band" from a vector?

Thanks to all for the suggestions. I developed something (to see if I could it) which turned out to be similar-ish to the suggestions made by LucMeekes. I do like the other suggestions too because a/ I learned something new b/ could fit nicely (i.e. 1 line) in the main program I may have to write

Given a vector fvec of length nf, I may have to do the following

for each value in nf

f = fvec(nf)

find value in band centred at: fband= inbandvalue(f,tol)

for each value in  fband

do something

next

next

so keeping some of the lines to just "1 call" to another small function is very handy.

Thanks everyone

Regards

Announcements