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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

return an array from another array

nvanson
1-Visitor

return an array from another array

Hello everyone,

I have a question from extracting an array.

Suppose I have an array A=[0 0 0 0 1 1 1 1 1 1 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2]

How can I extract a subarray B=[0 1 3 4 2]

I am new to mathcad. Thank you for your help in advance.

Son

ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald III
(To:nvanson)

NGUYEN Van Son wrote:

Hello everyone,

I have a question from extracting an array.

Suppose I have an array A=[0 0 0 0 1 1 1 1 1 1 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2]

How can I extract a subarray B=[0 1 3 4 2]

I am new to mathcad. Thank you for your help in advance.

With a bit of programming.

Stuart

View solution in original post

11 REPLIES 11
StuartBruff
23-Emerald III
(To:nvanson)

NGUYEN Van Son wrote:

Hello everyone,

I have a question from extracting an array.

Suppose I have an array A=[0 0 0 0 1 1 1 1 1 1 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2]

How can I extract a subarray B=[0 1 3 4 2]

I am new to mathcad. Thank you for your help in advance.

With a bit of programming.

Stuart

Slight alternative to Stuarts method using a while loop.

Mike

hello Mike,

Thank you very much for your response.

Are the elements of array: Duplicate3(M)=(0 1 2 3) taken from the last value (I underlined below) of each similar sequence? or arbitrary?

M:= (0 0 0 0 1 1 1 1 2 2 2 3 3 3 3 3 3)

If no, how can we know that?

And is the commande V<-- sort(M) is used to sort M?

If yes, I dont need this because I want an array randomly , like N:=(0 0 0 8 8 8 8 10 10 10 5 5 5 5 5 5 1 1 1 1 1 1 1 )

Thank you very much,

NGUYEN

StuartBruff
23-Emerald III
(To:nvanson)

NGUYEN Van Son wrote:

...

And is the commande V<-- sort(M) is used to sort M?

If yes, I dont need this because I want an array randomly , like N:=(0 0 0 8 8 8 8 10 10 10 5 5 5 5 5 5 1 1 1 1 1 1 1 )

Then you may want an approach something like the attached to get the replicated/singleton partition.

The problem with result this is that you have lost the ordering information.  If you want to retain the ordering but keep track of the sequence lengths, then something like this may be of use (based on my setcount function).

Stuart

Mike Armstrong wrote:

Slight alternative to Stuarts method using a while loop.

Mike

I notice you've got ORIGIN as the index of the first element of V; you can simplify it by just setting the index to 0.  Unfortunately, the rows trick for autoincrementing the row index doesn't work with an arbitrary ORIGIN.   Here's a variant of my original function that seems to be ORIGIN independent and the works over a 2D matrix (assuming standard Mathcad row ordering and that the values are continuous over column boundaries).

Stuart

Hello Stuart,

Thank you very much. Could you please help me how to return a such matrix, but the element of new array is choosen from the last value (underlined) of each similar sequence?

For ex: a:= (0 0 0 0 1 1 1 1 1 1 3 3 3 3 3 3 3  4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2)

then: stepvalues(a):= (0 1 3 4 2)

A bit more complicated but I hope you could pls give me a help.

NGUYEN

StuartBruff
23-Emerald III
(To:nvanson)

NGUYEN Van Son wrote:

Hello Stuart,

Thank you very much. Could you please help me how to return a such matrix, but the element of new array is choosen from the last value (underlined) of each similar sequence?

For ex: a:= (0 0 0 0 1 1 1 1 1 1 3 3 3 3 3 3 3  4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2)

then: stepvalues(a):= (0 1 3 4 2)

A bit more complicated but I hope you could pls give me a help

I'm having a little trouble understanding the difference between taking the number from the first value or the last value in the sequence - it's the same number, as far as I can tell, and I don't know how to distinguish between them, and the new output looks the same as the old output.  Could you please explain in more detail what your end goal is (that is, why you need to do this particular selection)?

Thanks,

Stuart

Hello Stuart and Werner,

PTC.jpg

I wanted to draw a curve from the step grapth like the image (Green curve). So I am successful with Stuart's instruction (Blue curve). It is now OK.

Sometimes, particular selection of end points of each step is important. But now it is enough for me. My problem is solved.

Thank you all very much.

Son

Werner_E
25-Diamond I
(To:nvanson)

Could you please help me how to return a such matrix, but the element of new array is choosen from the last value (underlined) of each similar sequence?

For ex: a:= (0 0 0 0 1 1 1 1 1 1 3 3 3 3 3 3 3  4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2)

Can you post an example where choosing the last values and choosing the first values will make a difference.

Hi,

Sometimes, we encounter this array:

a:= (0 1 0 0 0 5 4 6 5 2 1 2 2 4 1 5 4 4 4 )

return: b:= (0 1 4)

Sorry I did not explain clearly at the beginning.

Thank you.

Son

A slight alternative which will return the duplicated values and non-duplicated values. No what was requested but could be of some use.

Mike

Announcements

Top Tags