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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Array column creation with if statement?

dsochor
10-Marble

Array column creation with if statement?

The attached Mathcad Prime 3.1 worksheet (and screenshot) adds a 4th column to the array (or is it a matrix?) with the weight of pipe filled with water per foot. Creating the forth column with a definition works. The if statement tries to accomplish the same goal, what am I doing wrong?

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:dsochor)

Either use a different (dummy) variable instead of w.pipe[i,3 or even better omit the local assignment and use just the right hand side expression on its own.

You should also add an else statement in case a value in the first column is zero.

View solution in original post

14 REPLIES 14
Werner_E
25-Diamond I
(To:dsochor)

Either use a different (dummy) variable instead of w.pipe[i,3 or even better omit the local assignment and use just the right hand side expression on its own.

You should also add an else statement in case a value in the first column is zero.

-MFra-
21-Topaz II
(To:dsochor)

you can do so too:

wpipe.jpg

to which is added the case where w = 0

dsochor
10-Marble
(To:-MFra-)

Thank you both very much. F.M. that's a clever way to handle a 1 or 0 Origin.

Werner_E
25-Diamond I
(To:dsochor)

Yes, but to be fully ORIGIN-independent one has to change the second indices 0,1,2,3 to ori, ori+1, etc., too. Otherwise you will run into an error in case ORIGIN is not 0.

BTW, here's another way to achieve what you want using the if-command. I changed two values in the first column to zero to show the effect of the "else"-branch.

-MFra-
21-Topaz II
(To:Werner_E)

wpipe 1.jpg

Werner_E
25-Diamond I
(To:-MFra-)

Yes, now its ORIGIN-independent.

Because your routine is missing an else statement, you will get a zero in the  last column if  there is a zero in the first one (nor sure what really should happen in that case as the OP did not state). But in the (I guess very rare) case that the first column is all zero, no fourth column will be created at all. Not sure if that is desirable.

As I think that a program should deal with all possibilities (even if extremely rare), I would add an else branch.

-MFra-
21-Topaz II
(To:Werner_E)

wpipe 2.jpg

Werner_E
25-Diamond I
(To:-MFra-)

I am not sure if the whole calculation should fail just because of one zero in the first column, but this sure is also an option.

We don't know what Daniel had in mind, so every solution is OK.

dsochor
10-Marble
(To:-MFra-)

Thank you for continuing to work on this. Can you tell me what the purpose is of this part of the program?

wpipe+2.jpg

I should add that the program won't see a zero in the first column but it's good to add that option.

-MFra-
21-Topaz II
(To:dsochor)

Hi

In fact you did not say what happens if wpipe is zero.

So, I did that change which, it seems clear, in case that  the first column (I should have to write 3 + ori) there is a zero, the loop stops and sends the message that is displayed in the bottom of the image .

Greetings

dsochor
10-Marble
(To:dsochor)

A quick question; what change do I need to make to the last program return a vector of values instead of a single value?

LucMeekes
23-Emerald III
(To:dsochor)

I think this should work to get the full vector of k-values:

Success!

Luc

Thank you!

LucMeekes
23-Emerald III
(To:dsochor)

You're welcome.

Announcements

Top Tags