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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

matrix and programming

ptc-4915476
1-Newbie

matrix and programming

Hi all,

need some help again.

How do I achieve the same thing the first expression does with a programming loop?

matrix 1.PNG

I added my "solution" but something is missing.

The final goal will be of course to add some conditions inside the loop but I can't get even this working.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Right click at the upper left right corner and chose extended editor. Now you can add files to your post. You are not limited to Mathcad worksheets .

I've missed your question about the two vectors. If you need to change values in both vectors I would keep them (or bring them via augment) in a nx2 matrix which then would be the output of the function.

As I understand your second vector does not change, so your function has two input argument - the second vector as condition, the first providing the values - and one result - the changed vecor, which you may assign to a new varible or back to the first again. See examples for inverting and ignoring the unwanted values. In the first values over 2.5 are inverted, in the second only values <= 2.5 were put in the result vector.

matrixprog1.png

Of course you could do it without a function as in your first post but I usually try to keep things nice and tidy and as selfcontained as possible. Somewhat shorter but not that versatile and the names of the vectors A, B cannot be changed unless you change the proram, too.

matrixprog2.png

View solution in original post

16 REPLIES 16

When asking here it helps a lot and makes people more willing to work on your concern if you post a worksheet - preferably shrinked down to the barebones to make it easier for all of us.

It seems you want to copy one variable into another, presumably a twodimensional matrix. Doing it element by element you would use nested for-loops.

Are you looking for something like this:

zuweis1.png

Hello,

I have 2 vectors, to follow your example let's say it is A and F.

In your example, if I wanted to replace "9" with a value in F (my condition vector), how do I do this?

I would have to write for example:

a(Y, C):= ....(FOR loops)

But then B:=a(A) wouldn't work anymore. And B:=a(A,F) doesn't make sense because I want B also to be a vector and not a matrix.

Thanks

Your loop has no output.

Try renaming the input variables of the function x1 and x2. Once the loop finishes, the variables used in the loop do not exist.

What are you trying to do?

Thank you both for your answers. I'm going to continue working on this on monday.

Werner, thanks for the hint regarding the worksheets. I'll do that for future posts.

You're right, I want to copy one variable into another. There will be some conditions for doing that, that's why I want to use a programming loop.

The nested FOR loops look promising, I'll try that.

Regards

You're right, I want to copy one variable into another. There will be some conditions for doing that, that's why I want to use a programming loop.

That was the reason I let the routine replace every number >9 by "too big" and every 0 by 100. You might consider doing the datamanipulation outside in an extra function, depending on what the conditions are.

The nested FOR loops look promising,

Depends upon what you are trying to achieve. Your program looked like you are trying to copy anumber of rows determined by a third matrix from one Matrix to another.

According my routine you probably know the the use of ORIGIN serves the purpose of being independent from what ORIGIN is set to (Default: 0). Some people feel more comfortable with ORIGIN set to 1 and use routines which assume the default 0. Usually that means trouble.

Ok, maybe somebody can bring me on the right track.

It seems I cannot add a csv here? Only pictures, links and videos? My csv has 2 columns. Whenever the value in the second column is higher than 2.5 I want the value in the first column to be inverted (or ignored).

I had the two columns of this matrix assigned to two vectors, therefore my previous question how to pass two vectors to the programming function if I only want to change one of them.

sensor+outp+vs+SR.PNG

Right click at the upper left right corner and chose extended editor. Now you can add files to your post. You are not limited to Mathcad worksheets .

I've missed your question about the two vectors. If you need to change values in both vectors I would keep them (or bring them via augment) in a nx2 matrix which then would be the output of the function.

As I understand your second vector does not change, so your function has two input argument - the second vector as condition, the first providing the values - and one result - the changed vecor, which you may assign to a new varible or back to the first again. See examples for inverting and ignoring the unwanted values. In the first values over 2.5 are inverted, in the second only values <= 2.5 were put in the result vector.

matrixprog1.png

Of course you could do it without a function as in your first post but I usually try to keep things nice and tidy and as selfcontained as possible. Somewhat shorter but not that versatile and the names of the vectors A, B cannot be changed unless you change the proram, too.

matrixprog2.png

Thanks a lot Werner, I'll try out the For-loops as well.

I'm still doing babysteps with Mathcad.

Werner Exinger schrieb:

Right click at the upper left right corner and chose extended editor. Now you can add files to your post. You are not limited to Mathcad worksheets .

OK, it's the UPPER RIGHT corner!

Got it. This will ignore the 5ms set/reset pulse.

loop.PNG

If you spot an error please tell.

Thanks again for having a look at this. All your comments are highly appreciated.

While you posted I had updated my last post and attached a worksheet.

Looking at your program I spot the following - the only problem is 3.:

  1. X ist not initialized. While this normally would not do any harm I would set it to 0 at the beginning
  2. It seems you are preferring while-loops over for-loops. Nothing bad there, its just that for-loops are more compact and its seems appropriate to me to use them when I know how often the commands have to be executed.
  3. You wrote in your first post that you would invert or ignore the value in Y if C is greater than 2.5 (or > 1V in your example). You do neither of the two (or rather I had misunderstood your meaning of ignore) but replace that value in Y by the value 6 elements before. You will know why, but you may run into troubles if you would have to do that in the first 6 elements (index 0 to 5) as index i-6 is not valid in that case and you would get an error. If that replacement is mandatory you would either let the first 6 elements unchanged in every case and begin your loop with i=6 or you have to program i=0..5 as special case.
  4. You do not need X but can use Y instead and omit the else branch (and forget 1. )

Werner

1. Will do that.

2. I will try the for-loop as well

3. Yeah, I meant "replace", not ignore. The same as if I had used a sample-and-hold circuit right before I set the set-reset pulse to 5V. So I'll just use the values I obtained right before this pulse. Fortunately it doesn't occur at the very beginning.

4. Great! Do you mean like this? (I had to add the elseif because of some delays in my circuit)

loop.PNG

3. Yeah, I meant "replace", not ignore. The same as if I had used a sample-and-hold circuit right before I set the set-reset pulse to 5V. So I'll just use the values I obtained right before this pulse. Fortunately it doesn't occur at the very beginning.

4. Great! Do you mean like this? (I had to add the elseif because of some delays in my circuit)

ad 3) you must be sure about this, otherwise the program fails. On the other hand, if you are sure and your dataset is quite latrge, as I remember, why not begin with i=7 to be on the sure side?

ad4) Yes, while I'm not sure if you really want what you program. Lets say date C20 is greater than 1V and the surrounding values are not. Then the series Y19, Y20, Y21 would be replaced by Y13, Y20, Y14. Y20 remains - do you really want this to happen. Or do you mean something like

if Ci>1V then do (Yi-1=Yi-7; Yi=Yi-6; Yi+1=Yi-5)

?

C is bigger than 1V for 5ms , that's 5 samples or 5 lines in the CSV.

I want to replace the values in Y when C is bigger than 1V and also 1ms (1 line) before and after. That's why I added the elseif.

Does this make sense?

Steve Paul schrieb:

C is bigger than 1V for 5ms , that's 5 samples or 5 lines in the CSV.

I want to replace the values in Y when C is bigger than 1V and also 1ms (1 line) before and after. That's why I added the elseif.

Does this make sense?

I guess, no.

A rather quick and dirty hack would be:

if (Ci > 1V) THEN DO (Yi-1 = Yi-7; Yi=Yi-6; Yi+1=Yi-5)

You would do 8 unnecessary assigment that way but is easy to implement

I just look at your solution and as long as the pulse is longer than 1 ms it would work, too (with no unnecessary assignment but the elseif will cost time, too). In your solution the first 5 values are replaced with index-6 while the last two with index-7. So the fifth and sixth elelement of the replaced will be equal.

Another way would be to replace all 7 corrsponding values when you come to the first Ci>1 and then skip the next five values. This would be better done with the while loop.

Even better perhaps as follows:

i = 8

while i<rows(C)-1

if (Ci>1V)

while (Ci>1V)

Yi-1 = Yi-8

i=i+1

Yi-1 = Yi-8

Yi = Yi-7

i = i+1

return Y

The inner while loop replaces the first five values, the last two are done afterwards. It should work if the set/reset pulse is more or less than 5 data values, too.

As you replace a series of seven values, I replace every Yi by Yi-7. Otherwise (Yi-6) the first and the last of the seven replaced values would be the same.

Ok, I understand. What I should have said before is that this test is really long and the values change only every second. Meaning the 7ms before the C pulse I'm sure the value doesn't change (not considering noise) so it actually doesn't matter what exact value from the last 10 or so I replace Y with.. they will all be equal.

I'm lucky I don't have to do an interpolation between the value before the pulse and after the pulse, that would complicate things.

Top Tags