Post Reply 
Apply function to elements of a matrix column
08-30-2015, 10:54 AM (This post was last modified: 08-30-2015 10:55 AM by DrD.)
Post: #1
Apply function to elements of a matrix column
Replace column 5, element by element, with its absolute value:

M0:=[[1,1,0,1,2,1,0,0],[1,2,1,2,-5,0,0,1],[1,3,2,3,-6,0,0,1],[1,4,3,0,-3,0,0,1],[2,1,3,2,-6,0,0,1],[2,2,2,4,-2,0,0,1],[2,3,4,5,-5,0,0,1],[2,4,5,3,4,1,0,0],[3,1,5,4,5,0,0,1],[3,2,4,6,-2,0,0,1],[3,3,6,7,-8,0,0,1],[3,4,7,5,-8,1,0,0]];

desired result:

M0:=[[1,1,0,1,2,1,0,0],[1,2,1,2,5,0,0,1],[1,3,2,3,6,0,0,1],[1,4,3,0,3,0,0,1],[2,1,3,2,6,0,0,1],[2,2,2,4,2,0,0,1],[2,3,4,5,5,0,0,1],[2,4,5,3,4,1,0,0],[3,1,5,4,5,0,0,1],[3,2,4,6,2,0,0,1],[3,3,6,7,8,0,0,1],[3,4,7,5,8,1,0,0]];

This has been my best success, but is there a simpler way to apply a function to the elements of a column (or row)?:

[CAS]
M0(-5):=apply((x)->ABS(x),M0(-5));

-Dale-
Find all posts by this user
Quote this message in a reply
08-30-2015, 09:06 PM
Post: #2
RE: Apply function to elements of a matrix column
M0(-5):=abs(M0(-5))

Works in CAS.

Road
Find all posts by this user
Quote this message in a reply
08-31-2015, 08:26 AM
Post: #3
RE: Apply function to elements of a matrix column
Sorry to annoy you again. I try really hard to get used to my calculator. It is only there are weeks or mounths when I don't have time because of work and family.

I asked about cas in home in an other thread. To be honest I am not fully satified with the answer or to say the facts, but that is not the point here.

But what is going on here, again? I learned that home is for numerial stuff and cas for anything with undefined vars involved. That is in a way annoying when you find out you have to switch, but why would the abs command behave totally different in cas and in home??? Is it that I should use cas for numerical marix (manipulation)? I'm anxious about further posts!
Find all posts by this user
Quote this message in a reply
08-31-2015, 10:58 AM (This post was last modified: 08-31-2015 11:09 AM by DrD.)
Post: #4
RE: Apply function to elements of a matrix column
(08-30-2015 09:06 PM)roadrunner Wrote:  M0(-5):=abs(M0(-5))

Works in CAS.

Road

This format can result in "Error: Bad argument type," because the command line parser converts M0(-5):=abs(M0(-5)) to M0(-5):=|M0(-5)|. When M0(-5):=abs(M0(-5)) is recalled to the command line via Ans, (or even by copy/paste from the above posting), the command line parser converts from abs() to | |, which fails. I'm sure authors are aware of this potential bug, but negative indices are not supported in CAS at this time. So this inconsistent behavior isn't likely to get fixed soon.

-Dale-

Update Note:

I suppose one could transpose the matrix and apply a function to rows, instead of columns. This idea further complicates what should be a simple process. Working with matrices as a data field, such as when performing Kirchhoff network solutions (my current interest in the subject), makes (transpose - apply row changes - transpose - further processing) not a very pleasant solution.
Find all posts by this user
Quote this message in a reply
08-31-2015, 02:08 PM
Post: #5
RE: Apply function to elements of a matrix column
Quote:This format can result in "Error: Bad argument type,"
I also noticed that. I probably should have mentioned that I had to type it in each time, copy and paste or getting ABS from the toolbox didn't work.
Find all posts by this user
Quote this message in a reply
08-31-2015, 03:07 PM
Post: #6
RE: Apply function to elements of a matrix column
(08-31-2015 08:26 AM)leprechaun Wrote:  I try really hard to get used to my calculator. It is only there are weeks or mounths when I don't have time because of work and family

My sentiments, exactly. Smile
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)