HP Forums
Extracting values from vectors/matrices in home - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Extracting values from vectors/matrices in home (/thread-5755.html)



Extracting values from vectors/matrices in home - retoa - 02-24-2016 10:15 AM

Hello !

How can I extract a specific value from a vector or a matrix in home?

In cas I can extract the 2 value of a vector simply with [2 4 6](2) and I get 4.
If I do the same in home I get [4 8 12], so [2 4 6](2) will be interpreted as [2 4 6]*(2)
Is there a way to do this in home without having to store the vector in a variable?

Typical use is when you solve a polynomial equation with proot() or a linear system with RREF() and want to extract the single solutions.

Thank you very much for the help

Reto


RE: Extracting values from vectors/matrices in home - DrD - 02-24-2016 12:34 PM

Reserved variables are great for this kind of thing, but you said you don't want to use variables, so how about this, in HOME:

col([2,4,6],2); // ==> [4]

-Dale-


RE: Extracting values from vectors/matrices in home - Didier Lachieze - 02-24-2016 01:05 PM

CAS("[2 4 6](2)") will work in HOME and return 4 but I'm afraid it's not what you're looking for. Using a dedicated variable may be simpler.


RE: Extracting values from vectors/matrices in home - Han - 02-24-2016 04:06 PM

Just save your vector or matrix into ad global variable. For example:

M1:=proot( [1,5,6] ); // now M1 contains the vector [-3 -2]
M1[1] returns -3 and M1[2] returns -2

For matrices, use M1[m,n] to extract the element in row m and column n.
You can optionally use the following syntax: M1(1) and M1(2), or M1(m,n) for a matrix but I do not recommend this syntax because (esp. in CAS programs) it is not clear if something like:

myvar(m,n)

should be interpreted as a 2-variable function myvar with inputs m and n, or as the m,n entry of a matrix named myvar.

I know you specified not using variables, but global variables exist whether you use them or not, so I say go ahead and use them.


RE: Extracting values from vectors/matrices in home - retoa - 02-25-2016 08:05 AM

(02-24-2016 04:06 PM)Han Wrote:  Just save your vector or matrix into ad global variable. For example:

I know you specified not using variables, but global variables exist whether you use them or not, so I say go ahead and use them.

I use global variables for values I want to save for future use, and there are only 10 already defined global variables for matrices, I know I could try to always let one of them unused so that I can use it for this scope, but then they will be 9...

Storing a vector/matrix in a variable always implies more steps.

So the way proposed by Dale is probably the best one, as I can assign "col()" to a user key.
It would be nice to use col(ans,2), but it looks like "col" uses the Cas Ans and not the home one.
Same thing with CAS("Ans(2)"), this is quite logical as I am using CAS()

Is there a way to use col() in RPN mode?


RE: Extracting values from vectors/matrices in home - retoa - 02-25-2016 08:27 AM

(02-25-2016 08:05 AM)retoa Wrote:  Is there a way to use col() in RPN mode?

I found it, I have to enter the column number and then "col(2)", to specify that the col() command should use as arguments the first 2 stack elements.