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

4-Participant

## Vector Element - wise If programming

Hi,

I have vector and I need to check each element with a condition and the resulting answer should be a vector with the corresponding values if condition is true or false.

Problem: I have a vector called M' with 100 elements in it. I want to check the condition "If (M'<1)". If the condition is true it should assign a new vector Me with M' value in the corresponding position, If the condition is false it should assign 1 to the new vector Me for the corresponding position. I am getting an error stating that the value must be scalar.

I have attached a picture for your reference

1 ACCEPTED SOLUTION

Accepted Solutions
24-Ruby V
(To:surya93jith)

In addition to what Luc already has said, here are a few more ways to achieve what you probably want. The first turns the condition you want to use in a function which will work for a single scalar. Then this function is called vectorized with the vector M' as argument. See here three ways to write this function, either using the if-function or the programming if-statement:

Your approach would work if you really do it element wise and you would have to use a for-loop to do so. Furthermore in your appraoch you had the return statement in the else branch rather than at the very end.

Again I would rather turn the whole thing in an utility function which could easily be used for different vectors:

2 REPLIES 2
23-Emerald III
(To:surya93jith)

First off:

{You may have to ZIP it first, there's a bug on this forum that often causes a failure to attach a worksheet.

To zip, in Windows explorer select the worksheet file, then with the right-mous button select 'Send to'=> 'compressed (zipped) folder' the resulting .zip file can be attached.}

Then:

If you want to work element-wise, then do work element-wise.

You CANNOT successfully compare an array with a scalar. No matter how you try, mathematically NO vector can be equal to e.g. 1.

If you want to use a program for doing this element-wise, then one way is to individually access every vector element through its index, and make the comparison.

But you may not need a program at all. Below are three examples that do what I guess you intended your program to do:

The last example may appear to contradict what I said about comparing vectors with scalars at first sight. However it makes use of the vectorize operator (note the arrow above the expression) of Mathcad, that turns a single operator into a series of that operator applied to each individual element of a vector.

Success!
Luc

24-Ruby V
(To:surya93jith)

In addition to what Luc already has said, here are a few more ways to achieve what you probably want. The first turns the condition you want to use in a function which will work for a single scalar. Then this function is called vectorized with the vector M' as argument. See here three ways to write this function, either using the if-function or the programming if-statement:

Your approach would work if you really do it element wise and you would have to use a for-loop to do so. Furthermore in your appraoch you had the return statement in the else branch rather than at the very end.

Again I would rather turn the whole thing in an utility function which could easily be used for different vectors:

Announcements