Question
Posts: 167
Member Since: ‎03-10-2013
Message 1 of 6 (1,861 Views)

# HP50g - subset of a matrix - how to

Could somebody give an example how to extract a subset of a matrix, say extract a double column matrix from NxM matrix?

I have data in NxM matrix and want to perform approximation with LAGRANGE command, which needs a 2 column input data.  For example from 3x4 matrix

1    1     1      2

2    4     8      4

3    9    27    6

extract columns 1 & 3,  ie. get 3x2 matrix

1     1

2     8

3     27

Thanks

Highlighted
Posts: 367
Member Since: ‎02-04-2014
Message 2 of 6 (1,850 Views)

## Re: HP50g - subset of a matrix - how to

I doubt this is the simplest way, but at least it works.  Just put your matrix on the stack then do this:

DUP

{1 3} {3 3} SUB

{1 2} SWAP REPL

{1 1} {3 2} SUB

--> [[1 1] [2 8] [3 27]] as requested.

For more information about the SUB and REPL commands, see the Advanced Users Reference Manual, pages 3-242 and 3-199, respecively.  Other useful commands for column work are ->COL, COL->, COL-, and COL+ (pages 3-39 and 3-40).

-Joe-
Posts: 167
Member Since: ‎03-10-2013
Message 3 of 6 (1,829 Views)

## Re: HP50g - subset of a matrix - how to

Thank you Joe. Your solution works. After a few tests I picked another way with CSWP command. For the special case above it would be something like

<< 2 3 CSWP {1 1} {3 2} SUB >>

In general it could be something like

<<  -> T I

<< T 2 I CSWP DUP

SIZE 1GET 2 2 ->LIST

{1 1} SUB

>>

>>

Program takes 2 parameters: matrix T and column number I, then extracts two columns (1 and I) from T matrix. In column 1 are x'es, in other columns are values for approximation. The line with SIZE, GET and ->LIST  is to detetrmine number of rows of the matrix (I don't know simpler way :-)

BTW, SCATRPLOT takes 2 columns from NxM matrix

Thank you for the hint.

PhD Student
Posts: 871
Member Since: ‎08-10-2010
Message 4 of 6 (1,803 Views)

## Re: HP50g - subset of a matrix - how to

You can also use the COL- command ( MTH  MATRIX  COL  COL- ).

Removes the desired column number from the matrix, returning new matrix and deleted column as a vector.

Here's a simple program.

Matrix is on Level 2 and a list of columns to delete is on level 1:

<<

SORT LIST-> -> n

<<  n 1 + PICK

1 n FOR j

SWAP COL- DROP

NEXT

>>

>>

So this:

returns this:

-Bart
_________________________________________________________
calculator enthusiast
Posts: 367
Member Since: ‎02-04-2014
Message 5 of 6 (1,783 Views)

## Re: HP50g - subset of a matrix - how to

Here's another approach to Bart's program.  Same inputs.

<< SORT REVLIST 1 << COL- DROP >> DOLIST >>

Unlike Bart's program, however, this only returns the new (smaller) matrix; the original is lost.

-Joe-
PhD Student
Posts: 871
Member Since: ‎08-10-2010
Message 6 of 6 (1,772 Views)

## Re: HP50g - subset of a matrix - how to

Joseph Horn wrote:

<< SORT REVLIST 1 << COL- DROP >> DOLIST >>

Brilliant solution Joe. I sometimes forget the 50g has recursive commands like DOLIST.

Joseph Horn wrote:

Unlike Bart's program, however, this only returns the new (smaller) matrix; the original is lost.

That's because I used PICK instead of ROLL, but usually only the answer is required anyway - so your solution is "cleaner" :-).

-Bart
_________________________________________________________
calculator enthusiast
† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation