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

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

The array order reassign

KC_10577235
5-Regular Member

The array order reassign

Hi Masters

How to reassign the array elements order??

tks

KC_10577235_0-1676297286626.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
lvl107
20-Turquoise
(To:lvl107)

2.PNG

   Best Regards.

View solution in original post

8 REPLIES 8

Use C:=stack(A,B)

Ooops, I should have used my glasses, indeed!
Sorry! See the reply of @lvl107  for a correct solution.

lvl107
20-Turquoise
(To:KC_10577235)

It seems like this :

1.PNG

Best Regards.

   Loi.

lvl107
20-Turquoise
(To:lvl107)

2.PNG

   Best Regards.

Werner_E
24-Ruby V
(To:lvl107)

I guess this is just my second appraoch with differently named variables and without being ORIGIN-independent and without error checking, right? 😉

KC_10577235
5-Regular Member
(To:lvl107)

Hi Lvl107

Could you explain the logical operation of R.rows(R) ??

tks

 

 

R is a variable which ultimately contains the return value of the function (R ... Result, Return value), here its a vector.
When a variable is used in a program for the first time on the left hand side of a local assignment, it is automatically initialized with zero.
The rows(...) function, which actually returns the number of rows of a matrix, returns 0 for a scalar like 0. Therefore, the first time R[rows(R)<- ... creates a 1x1 matrix R. The next time rows(R) returns the value 1 and so with R[rows(R)<- ... another element is added to the vector R, etc.
However, the whole thing works only if the system variable ORIGIN has the value 0.
This is the reason why my function uses the expression ORIGIN+rows(R) instead of rows(R) (and I abbreviated ORIGIN in my program at the very beginning with O), so that the function also works in worksheets which use a different value for ORIGIN (many users set Origin to the value 1, so that with vectors the first element has the index 1 and not 0).

 

In short - R[rows(R) is a way to create a vector and add values without having to initialize the vector (which you would have to do in an extra step if you would use R <-- stack(R, ...) to add elements to the vector.

Here is a generic utility function which interleaves two vectors of arbitrary lengths. They do not necessarily have to be the same length.

Werner_E_0-1676310554456.png

File in Prime 6 format attached

 

EDIT: Added a slighter shorter variant

Werner_E_0-1676311116288.png

 

 

This isn't better than the others already posted, just different.

 

2023-02-13_14-40-07.png

Or a small variation:

2023-02-13_14-43-57.png

Top Tags