cancel
Showing results for 
Search instead for 
Did you mean: 
eric746
New member
1 0 0 0
Message 1 of 4
1,453
Flag Post

Solved!

How to apply a function to each element of a matrix?

HP Recommended
HP 50g

How to apply a built-in function, such as [COLLECT()] to all elements of a list, vector or matrix?

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
BartdB
Level 9
971 954 119 186
Message 3 of 4
Flag Post
HP Recommended

Hi,

 

MAP can be applied directly to a matrix:

 

[[ 1   2   3 ]
 [ 4   5   6 ]
 [ 7   9   9 ]]
<< SQ >>
MAP

will give

[[ 1   4   9  ]
 [ 16  25  36 ]
 [ 49  64  81 ]]

 

See page 3-141 of the AUR ( HP-50G Advanced User's Reference )

 

-Bart
_________________________________________________________
calculator enthusiast

View solution in original post

Tags (1)
Was this reply helpful? Yes No
3 REPLIES 3
David_M
Level 6
143 142 29 62
Message 2 of 4
Flag Post
HP Recommended

Here's one way:

 

(assumption: RPN mode)

 

1) If your target object is a vector or matrix, issue the AXL command to convert it to a list.

2) Create a small program object containing the function you want to apply.  For example, if you wanted to apply the SIN function to all elements, create a program that looks like this:

« SIN »

3) Execute the MAP command.

4) The result is a list with the function applied to each element.  If you need to convert this back into a matrix/vector, just execute another AXL command.

 

Replace the SIN function in step 2 with whatever function(s) you wish.

 

Using your example, if I had a matrix that I wished to apply the COLLECT command to, I'd do the following:

AXL
« COLLECT »
MAP
AXL

For more information about the AXL and MAP commands, see the Advanced User's Reference which is available here.

 

Hope this helps!

 

- David

Was this reply helpful? Yes No
BartdB
Level 9
971 954 119 186
Message 3 of 4
Flag Post
HP Recommended

Hi,

 

MAP can be applied directly to a matrix:

 

[[ 1   2   3 ]
 [ 4   5   6 ]
 [ 7   9   9 ]]
<< SQ >>
MAP

will give

[[ 1   4   9  ]
 [ 16  25  36 ]
 [ 49  64  81 ]]

 

See page 3-141 of the AUR ( HP-50G Advanced User's Reference )

 

-Bart
_________________________________________________________
calculator enthusiast

View solution in original post

Tags (1)
Was this reply helpful? Yes No
David_M
Level 6
143 142 29 62
Message 4 of 4
Flag Post
HP Recommended

Of course you're right, Bart.  No need for the AXL.  I've been too list-focused lately! :generic:

Was this reply helpful? Yes No
† 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