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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

trimZeroElements( ) ?

lvl107
20-Turquoise

trimZeroElements( ) ?

Hello Everyone.
From :

1.PNG
Need help with the trimZeroElements( ) program function with ORIGIN := 1 and ORIGIN := 0 ?
Thanks in advance for the time and help.
Best Regards.
Loi.

31 REPLIES 31
Werner_E
25-Diamond I
(To:lvl107)

Werner_E_0-1708990877618.png

 

lvl107
20-Turquoise
(To:Werner_E)

I appreciate for your answer with .xmcd, mathcad file, Werner. 😊 ( I usually type a wrong key-stroke ! ) and :

1'a.PNG

and of course I would expect is :  

                                                           1''.PNG  !!

2.PNG

3.PNG

   In any case of INPUT and the OUTPUT is as the my question above ( yellow highlight )

       Best Regards.

            Loi.

Werner_E
25-Diamond I
(To:lvl107)

Werner_E_1-1708998350045.png

 

Really? The "3" is coming out of thin air. Its not in the input but appears in the output?

I can see no logic here! Why would the numbers be rearranged that way? Why does he "9" appear only once in the result? .....

 

And what would the desired output be here? 25 zeros and 11 non-zero numbers, all different.

Werner_E_2-1708998560558.png

 

You will have to clearly and precisely DESCRIBE what the behaviour of your magic function should be - not just providing one or two examples!

Otherwise I would, from what you posted so far, suggest the following function

Werner_E_4-1708999341558.png

 

 

lvl107
20-Turquoise
(To:Werner_E)

1'.PNG

 

Nobody can make no mistake !!

 

4.PNG

 

      Best Best Regards. 

Werner_E
25-Diamond I
(To:lvl107)

You still don't explain the exact behaviour of the desired function, just repeat similar examples over and over again, don't explain why the result is always a 3x3 matrix, don't say what the result with my example matrix with 11 non-zero values should be., ...

Because of all this, I feel unable to help in any way.
Perhaps someone else can guess what function you are looking for. In any case, I wish you good luck with it.

lvl107
20-Turquoise
(To:Werner_E)

11.PNG

   I'm appreciated about the words, the above, Werner.😊  And now :

My question is your own example, Test4 

9.PNG

and I guess I should repeat my quesion :

" solve Test4 with both of ORIGIN := 1 and ORIGIN := 0 ? "

10.PNG

     

           Best best Regards.

JKT
13-Aquamarine
13-Aquamarine
(To:lvl107)

You simply don't give enough information to generate the function you want, so it can't be done.

 

What are the requirements for the input matrix? Dimensions? Number of non-zero elements?

If the number of non-zero elements is open, how are we supposed to define even the output matrix dimensions?

lvl107
20-Turquoise
(To:JKT)

Hi, JKT.

I greatly appreciate your help with how to set-up my question :
I. Requirement for input matrix :
     1. Demension : 6x6
    2. Number of non-zero elements : 9
    3. Number of zero elements : 27

a.PNG
II. Requirement for output matrix :
    1. Demension : 3x3
    2. Number of non-zero elements : 9
    3. Number of zero elements : 0

b.PNG
III. Test the program function with both of case : ORIGIN := 1 and ORIGIN := 0

 

 Or in other words :

c.PNG

  Many thanks again for helping me how to set-up that my question, JKT 😊 

      Best Regards.

           Loi.

 

JKT
13-Aquamarine
13-Aquamarine
(To:lvl107)

There's still one thing that wasn't defined: should the input be read by rows or by columns. That can change the result. The following reads it by rows:

JKT_0-1709040094782.png

There is likely a much more simple way, but this seems to do the job.

 

lvl107
20-Turquoise
(To:JKT)

  I greatly appreciate for your response, JKT. 😊😊😊

  And please attach the Mathcad file .xmcd . ( for testing with ORIGIN := 1 and ORIGIN := 0 )

     Best Regards.

         Loi. 

JKT
13-Aquamarine
13-Aquamarine
(To:lvl107)

I didn't keep it - sorry!

It is just such a simple one that you can easily copy it. I did test it with several values of ORIGIN. (0, 1, 2 and 6 IIRC)

 

lvl107
20-Turquoise
(To:JKT)

I guess the above : " my surely believing " ( previous post ) comes true . 😊 😊 😊

d.PNG

and now I am hoping and waiting for another approach ...

   Thanks again,

       Loi.

   

Werner_E
25-Diamond I
(To:lvl107)

Limitation of the input to a 6x6 matrix was not stated clearly in your first postings.

The same applies to the strange limitation to exactly 9 non-zero numbers. Why didn't you answer that my test matrix with 11 non-zeros should throw an error because you just want the function to work on matrices with exactly 9 non-zeros?.

But I am glad that you are happy now.

Here is a shorter version - have fun!

Werner_E_0-1709058252881.png

For reading the numbers column wise simply replace both M^T by M.

Can you think of any practical use for a function so much  limited?
I couldn't, so I called it  useless( ).

 

JKT
13-Aquamarine
13-Aquamarine
(To:Werner_E)

Still shorter ... and I would assume that reading the matrix in columns would likely fill the result that way too.

JKT_0-1709102953578.png

I really need to learn to use Match effectively...

lvl107
20-Turquoise
(To:JKT)

   Hi, again JKT.

The output that I'm expecting as the following :

From :

A.PNGB.PNGC.PNG

   Thanks in advanced for your time and help.

       Best Regards.

            Loi.

Werner_E
25-Diamond I
(To:lvl107)

In contrast to the previous one, a replace function could even be useful from time to time.

I would implement it more generally, so that you can freely choose the element that should be replaced and what it should be replaced with. Can be done in one line ...

Werner_E_1-1709244964409.png

Or even more generic:

Werner_E_2-1709246974360.png

 

 

lvl107
20-Turquoise
(To:Werner_E)

   Many thanks, Werner.

  I guess it that's a shortest one !! and another query :

D.PNG

   Regards.

Werner_E
25-Diamond I
(To:lvl107)

Thats too specific and I see no reason and use for transforming a 6x6 matrix into a 4x9 matrix while replacing the zeros for something else.

So I won't deal with it, but you can try to do it very similar like the transformation of the 6x6 matrix into a 3x3 matrix by excluding exactly 27 zeros. You can doit using a couple of loops and counters or by playing around with the index numbers in the function you feed into the matrix() function as was shown by JKT.

I can hardly imagine that you really have a specific use for functions like that. And if you are just curious and your goal is to learn about programming functions in Mathcad, I would suggest that you give it a try yourself first and if you fail. show your attempt here. Its always better learning by doing than just by watching.

ttokoro
20-Turquoise
(To:lvl107)

image.png

Origin free version of Prime 9.image.pngimage.pngimage.png

lvl107
20-Turquoise
(To:ttokoro)

My Mathcad 15 says it that's OK, Ttokoro. 🙄 🤔

E.PNG

 

   Ttokoro, I wonder whether there's another approach without using matrix( , , ) and submatrix( , , , , ) built-in function ?

 

   Regards.

      Loi.

ttokoro
20-Turquoise
(To:lvl107)

image.png

Werner_E
25-Diamond I
(To:ttokoro)

I prefer smaller utility-functions that perform a general, narrowly defined task and then combine them as required.
lvl107's demanded very limited task of converting a 6x6 matrix into a 4x9 matrix and replacing the zeros with spaces does not justify a separate function in my opinion.
However, you can combine the "replace" function I have already presented with a "resize" function, which transforms any(!) matrix into a matrix with a specified number of rows - the original matrix is read row by row and the new matrix is written to in the same way, with any resulting surplus fields being filled with a value of your choice.

The functions shown here are implemented as one-liners. This is only because we have somehow drifted into a competition for the shortest function. However, I would advise against using such line monsters in productive operation, as they are very error-prone, **bleep** hard to read and therefore difficult to maintain.

EDIT: After sending my reply I noticed that the word "d_a_m_n" was replaced by "bleep" by this system here. Honestly? Funny! Sorry, Mom!
Is there a list of banned words published somewhere? Guess no, as the list would just consist of a myriad of "bleep"s. 😉

 

Werner_E_0-1709349954558.png

 

Werner_E_1-1709350006382.png

 

 

lvl107
20-Turquoise
(To:ttokoro)

   Many thanks, Ttokoro. 🤔 🙄 🤔

   The reason that I need help with that approach is that I have not been familiar with matrix( , , ) and submatrix( , , , , ) built-in functions yet. Similarly, there's another query :

F.PNG

  Best Regards.

Werner_E
25-Diamond I
(To:lvl107)

Just combine the already posted utility functions to get the results you want.

Werner_E_0-1709356043172.png

BTW, according to the standards, zero is considered to be a natural number as well.

 

Some important questions at last:
Are you heading towards a specific goal with all of your many different questions?
Is there any reasonable application for the very specific features you ask about?

What do all of these questions ultimately mean?

 

lvl107
20-Turquoise
(To:ttokoro)

Ttokoro, I'm wondering whether there's another similar approach that without using matrix( , , ) and submatrix( , , , , ) built-in function ?

N1.PNG

 

N2.PNG

 

 

   Best Regards.

lvl107
20-Turquoise
(To:lvl107)

Z1.PNG

Z2.PNG

 

Z3.PNG

 

JKT
13-Aquamarine
13-Aquamarine
(To:Werner_E)

Those could indeed be useful sometimes. However, the second one seems to break up in Prime, which is a real pity.

Werner_E
25-Diamond I
(To:JKT)


@JKT wrote:

Those could indeed be useful sometimes. However, the second one seems to break up in Prime, which is a real pity.


@JKT 

In which way would it break in Prime?

Haven't tried in Prime but I could imagine that you don't mean that the function itself fails but rather the way I used it in the examples posted, right?

 

The boolean functions which I provided as the second argument in some of the examples make use of Mathcad's way of evaluating boolean expressions. This is implemented quite efficient in Mathcad as it uses short-circuit evaluation.

If the first part in an expression like  IsString(x) ^ (x>9)  is already seen as "false", the second one is not evaluated at all, because its already clear that the whole AND-expression must yield a "false". So if x is a string, Mathcad never tries to compare it to the number 9. This comparison would throw an error as you can't compare a string to a number in size.

 

In Prime evaluation of boolean expressions is implemented differently and the second part of the expression is always evaluated, even though it would not be necessary. That's the reason Prime will throw an error when trying to evaluate "string">9.
In Prime therefore it would be necessary to use nested if-functions (or if you prefer, if statements) to make it work.

 

That said, I guess its not the function "Replace" which fails in Prime but rather the boolean functions which I provide and which would have to be formulated in a different way. Then I guess the main function "Replace" should work OK in Prime as well.

 

And, yes, I won't hesitate to admit that taking advantage of the way evaluations are implemented in a specific programming language may not be considered the best style in programming. But its so convenient that I couldn't resist 😉

 

Example in Mathcad:

Werner_E_1-1709304579532.png

 

Examples in Prime:

Werner_E_3-1709305479063.png

JKT
13-Aquamarine
13-Aquamarine
(To:Werner_E)

I don't have Mathcad at hand now, but the entire implementation of the function in the replace seemed to fail. It silently didn't replace anything, so it never found a match.

Announcements

Top Tags