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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Translate the entire conversation x

How to use stack function

YA_10963798
14-Alexandrite

How to use stack function

Hi there , 

I want to stack one value in a matrix in the correct order , but when I use stack function I can put it only in the beginning or at the end like this : 

YA_10963798_0-1765462294904.png

I want to put that -13.75 in the correct place which is in the middle in this case . How to use stack function and order the values in the same time 

Thanks 

 

ACCEPTED SOLUTION

Accepted Solutions

Soup is nicely bubbling (eye of newt; goes nicely with the toasted bat-wing sandwiches), so ... time for one more method.

 

2025 12 11 B.png

 

I added an extra test value to demonstrate that the order of the v & x values doesn't matter, just the indices.   You could also write a program that does essentially the same thing.

 

Oh, forgot to mention, neither stack nor augment inserts values into an array; they just prepend or append values.

 

<Caveat>I haven't properly validated the results.  You may wish to do so</Caveat>

 

Stuart

View solution in original post

8 REPLIES 8

Try stack followed by sort. (followed by reverse, if necessary).

YA_10963798
14-Alexandrite
(To:AlanStevens)

Can you explain more... BTW the value I want to insert may change depending on the input

 

Thanks 

Here's one way, provided you want to add an element to the bottom, top, or middle of a vector.

 

2025 12 11 A.png

 

There are other ways that more literally insert an element into a vector. 

 

Stuart

 

I'd add another standard form (in Express, so no programming), but I've almost learned the dangers of trying to program or read whilst dinner is cooking on the hob!

 

Soup is nicely bubbling (eye of newt; goes nicely with the toasted bat-wing sandwiches), so ... time for one more method.

 

2025 12 11 B.png

 

I added an extra test value to demonstrate that the order of the v & x values doesn't matter, just the indices.   You could also write a program that does essentially the same thing.

 

Oh, forgot to mention, neither stack nor augment inserts values into an array; they just prepend or append values.

 

<Caveat>I haven't properly validated the results.  You may wish to do so</Caveat>

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

Thank you Stuart and I hope the soup was delicious. 

Werner_E
25-Diamond I
(To:YA_10963798)

You will have to be more specific as to what you are trying to achieve. Your screenshot shows a lot of variable names which we do not know the values of.

So I would suggest that you simplify it.

From what I understand you have a vector of values and this vector already is in descending order,  right?

And all you want to do is to add a value to this vector  and you want the new vector to be in descending order again. Is this correct?

Then what's your problem with Alan's suggestion??? Have you even tried to follow it?

Werner_E_0-1765480822430.png

 

YA_10963798
14-Alexandrite
(To:Werner_E)

Hi Werner, 

I don't understand what Randy suggested this is why I asked for more clarification. Because sometimes my vector can have 10 values and the value I want to add may be in any order ( like the third of seventh) if I use reverse how can I guarantee that I will add the value in the accurate spot. 

 

Thanks 

Yusra 

Hi Yusra,

 

Given that your z vector is depth-related, a natural assumption is that adding a new value should maintain depth-order.   Hence, using my telepathic skills, Alan's suggesting the use of reverse after sorting.  Which leads to a slightly more straightforward solution.

 

2025 12 12 E.png

 

I wasn't sure whether you wanted to retain the depth order or needed to put a specific value at a particular index, hence the more general-purpose insertion suggestion.

 

Stuart

 

(The soup needed more garlic and less white pepper - the newts kept sneezing)

Announcements

Top Tags