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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Programming/Matrices

Heavenly
1-Newbie

Programming/Matrices

If I have two vectors each with one column and 15 rows and I want to take the minimum of each row of the two vectors and make that a new vector is that possible?

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:Heavenly)

Sure David,

Here's a solution.

MinRow.png

Now instead of the two fixed vectors, you can call MinRow with any set of vectors you like: MinRow(FirstVector, SecondVector)=...

You will get an error if the length/size of the vectors are unequal.

Success!

View solution in original post

11 REPLIES 11
LucMeekes
23-Emerald III
(To:Heavenly)

Sure David,

Here's a solution.

MinRow.png

Now instead of the two fixed vectors, you can call MinRow with any set of vectors you like: MinRow(FirstVector, SecondVector)=...

You will get an error if the length/size of the vectors are unequal.

Success!

min() cannot be vectorized, otherwise it would be a oneliner.

But we can define an auxiliary function using min() which then can be vectorize and does the trick without programming.

12.05.png

Thanks for the quick replies!! Both solutions work great. Since I am not very savy with programming or with using functions that are not on the basic toolbar I really apppreciate the help. I do have another question related this topic.

If I have three vectors A, B,C. Vector A is composed of lets say 5 entries which are either "red" or "blue". Vector B is all 1's and Vector C is all 2's. I want to return a 4th Vector that returns 1 if the entry in Vector A is red and returns 2 if the entry in vector A is blue.

Thanks.

What would you need vector B and C for?

Its always better to attach a worksheet so we don't have to type in all from scratch - helps to motivate providing a solution 😉

Here is a way to achieve what you described and to achieve what I think you wanted:

13.05.png

WE,

Attached is the file I am trying to get working. For example on pg. 7 I am trying to solve for Pn. I tried making a vector with 15 entries of 0.44 but that didnt work. A solution for this problem will fix the others in the worksheet. Thanks.

Dave

As in the attached (only Check1 is done - you should be able to do the rest)?

The same method is required for Pn.

Alan

Thank you very much. I did get everything else to work. Where do I find the epsilon symbol? Its not on the programming toolbar. Last question in the calculation for Pn on page 7 does it matter if the local definition variable is the same as the actual variable being defined?

Dave

David DePolo wrote:

Where do I find the epsilon symbol?

When you select "for" from the programming toolbar it is automatically inserted.

David DePolo wrote:

... does it matter if the local definition variable is the same as the actual variable being defined?

No. The local variable exists only within the programming construct.

Alan

AlanStevens wrote:

The local variable exists only within the programming construct.

A variable used inside a program will have the same value as one outside the program with the same name if it is previously defined, but changes made to it within the context of the program are not passed back out.

Basically correct - if you use a name at the RHS of an assignment and there is no local variable with that name, the value of an existing global variable of that name is taken. Whenever you use a name at the LHS of an assignment it is automatically made a local variable.

But you have to take care when using matrices - see below. The simple usage of the global variable makes a big difference.

16.05.png

While test1 and test2 show what one would expect, is the logic in test3 not immediately apparent. It looks like the simple usage of the global M creates a local duplicate like it was done explicitely in test2.

Interesting. That behavior in test3 is not what I would expect.

Top Tags