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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Manipulate Size of Arrays

xyz123
12-Amethyst

Manipulate Size of Arrays

Hello,

 

let´s say I have two arrays with the same length.

 

Also I have a function which wants to do further calculations with the arrays.

 

I need some kind of trick where I can ignore the first element of A and B. 

I tried a for-loop from k = 1 to length(B) but no... doesnt work this way.

 

xyz123_0-1649347967365.png   

 

I expect a result array like this with 5-1 = 4 elements (first row deleted).

In best case I need all the array manipulations within the function C.

 

xyz123_2-1649348378533.png

 

Thanks

 

 

ACCEPTED SOLUTION

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

Here's a function C that does what you want, for input vectors of any length >1, with two examples of usage:

LucMeekes_0-1649352595495.png

And it also works in Prime express...

 

Success!
Luc

 

View solution in original post

7 REPLIES 7
StuartBruff
23-Emerald III
(To:xyz123)


@xyz123 wrote:

Hello,

 

let´s say I have two arrays with the same length.

 

Also I have a function which wants to do further calculations with the arrays.

 

I need some kind of trick where I can ignore the first element of A and B. 

I tried a for-loop from k = 1 to length(B) but no... doesnt work this way.

 

xyz123_0-1649347967365.png   

 

I expect a result array like this with 5-1 = 4 elements (first row deleted).

In best case I need all the array manipulations within the function C.

 

xyz123_2-1649348378533.png

 

Thanks

 

 


Would something like this suffice? ...

 

2022 04 07 a.png

 

It should give the result of dividing the vectors, up to the shortest of the two and excluding the first element.  It doesn't check for a single-element vector.

 

Stuart

LucMeekes
23-Emerald III
(To:xyz123)

Here's a function C that does what you want, for input vectors of any length >1, with two examples of usage:

LucMeekes_0-1649352595495.png

And it also works in Prime express...

 

Success!
Luc

 

StuartBruff
23-Emerald III
(To:LucMeekes)


@LucMeekes wrote:

Here's a function C that does what you want, for input vectors of any length >1, with two examples of usage:

LucMeekes_0-1649352595495.png


 

And for those who prefer to stick to ORIGIN = 0, here are a couple of handy Mathcad Express functions that I keep in my default worksheet and that simplify calculations like this.

 

2022 04 07 e.png

 

Stuart

 

(subvector is easily made ORIGIN-independent, but I confess that it hurts my eyes to look at it and all those shouty capitals give me a headache 🤕)

xyz123
12-Amethyst
(To:LucMeekes)

submatrix works wonderful, thank you very much.

 

Also thanks to all other solutions.

 

👍

StuartBruff
23-Emerald III
(To:xyz123)


@xyz123 wrote:

Hello,

 

let´s say I have two arrays with the same length.

 

Also I have a function which wants to do further calculations with the arrays.

 

I need some kind of trick where I can ignore the first element of A and B. 

I tried a for-loop from k = 1 to length(B) but no... doesnt work this way.

 

xyz123_0-1649347967365.png   

 

I expect a result array like this with 5-1 = 4 elements (first row deleted).

 


Just some added comments:

 

1.  (Most important) Please attach a copy of the worksheet with the issue. 

a.  There are several potential causes of Mathcad errors that are either difficult or impossible to see from just an image (for example, what ORIGIN is set - that's why I made my function ORIGIN-independent).

b.  It's generally not possible to read the error messages, and hence significant clues may be missing.

 

2.  There's no need to predefine a for-loop variable.

 

3.  The Mathcad function length returns a single number.   The for loop will just assign 5 to k.   

 

Points 1 and 3 are demonstrated below:  

 

2022 04 07 b.png

 

The error in your program appears to be due to you having ORIGIN = 0, which means the maximum index is 4.  Consequently, k tries to index a non-existent element and Mathcad raises an appropriate error ... which you can't read the details of but would have been visible to the reader in the worksheet.

 

2022 04 07 c.png

 

4.  A Mathcad program returns the value of the last expression it calculated; in this case, just a[k/b[k.   If you want to return a vector, you need to assign the result of each operation to a variable.   You also need to take care that you use indices correctly.   In your example, you need to make the indices for a and b one greater than the return vector index.   Here's a slightly simplified version of the C function that assumes ORIGIN = 0 that takes into account the above:

 

2022 04 07 d.png

 

Hope that helps.

 

Stuart

Werner_E
25-Diamond I
(To:xyz123)

Here is an approach which catches all divisons by zero. By filterNaN of course you lose the information about which row throws an error.

Werner_E_0-1649357916740.png

 

StuartBruff
23-Emerald III
(To:Werner_E)


@Werner_E wrote:

Here is an approach which catches all divisons by zero. By filterNaN of course you lose the information about which row throws an error.

Werner_E_0-1649357916740.png

 


 

2022 04 07 g.png

 

Stuart

Announcements

Top Tags