Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X
Hello Everyone.
From :
Need help with the trimZeroElements( ) program function with ORIGIN := 1 and ORIGIN := 0 ?
Thanks in advance for the time and help.
Best Regards.
Loi.
I appreciate for your answer with .xmcd, mathcad file, Werner. 😊 ( I usually type a wrong key-stroke ! ) and :
and of course I would expect is :
!!
In any case of INPUT and the OUTPUT is as the my question above ( yellow highlight )
Best Regards.
Loi.
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.
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
Nobody can make no mistake !!
Best Best Regards.
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.
I'm appreciated about the words, the above, Werner.😊 And now :
My question is your own example, Test4
and I guess I should repeat my quesion :
" solve Test4 with both of ORIGIN := 1 and ORIGIN := 0 ? "
Best best Regards.
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?
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
II. Requirement for output matrix :
1. Demension : 3x3
2. Number of non-zero elements : 9
3. Number of zero elements : 0
III. Test the program function with both of case : ORIGIN := 1 and ORIGIN := 0
Or in other words :
Many thanks again for helping me how to set-up that my question, JKT 😊
Best Regards.
Loi.
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:
There is likely a much more simple way, but this seems to do the job.
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.
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)
I guess the above : " my surely believing " ( previous post ) comes true . 😊 😊 😊
and now I am hoping and waiting for another approach ...
Thanks again,
Loi.
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!
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( ).
Still shorter ... and I would assume that reading the matrix in columns would likely fill the result that way too.
I really need to learn to use Match effectively...
Hi, again JKT.
The output that I'm expecting as the following :
From :
Thanks in advanced for your time and help.
Best Regards.
Loi.
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 ...
Or even more generic:
Many thanks, Werner.
I guess it that's a shortest one !! and another query :
Regards.
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.
Origin free version of Prime 9.
My Mathcad 15 says it that's OK, Ttokoro. 🙄 🤔
Ttokoro, I wonder whether there's another approach without using matrix( , , ) and submatrix( , , , , ) built-in function ?
Regards.
Loi.
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. 😉
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 :
Best Regards.
Just combine the already posted utility functions to get the results you want.
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?
Ttokoro, I'm wondering whether there's another similar approach that without using matrix( , , ) and submatrix( , , , , ) built-in function ?
Best Regards.
Those could indeed be useful sometimes. However, the second one seems to break up in Prime, which is a real pity.
@JKT wrote:
Those could indeed be useful sometimes. However, the second one seems to break up in Prime, which is a real pity.
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:
Examples in Prime:
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.