Re: Matrix Output
- To: mathgroup at smc.vnet.net
- Subject: [mg71019] Re: [mg71001] Matrix Output
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Sat, 4 Nov 2006 23:07:17 -0500 (EST)
- Organization: Mathematics & Statistics, Univ. of Mass./Amherst
- References: <200611040908.EAA25111@smc.vnet.net>
- Reply-to: murray at math.umass.edu
In the context of Mathematica, I'm not sure I understand anything about
your question: are you sure this is the group you wanted to post to?
Your syntax is totally non-Mathematica.
. lists are enclosed within {curly brackets} and their entries
separated by commas -- thus "{1,4}" and NOT "[a 4]"
. "zeros" is not the name of a built-in Mathematica function
. all Mathematica functions enclose their arguments by
[square brackets] and NOT parentheses -- thus
"ZeroMatrix[4,2]" and NOT "zeros[4,2]"
. "E" is a reserved symbol for the base of the natural log
. The built-in Mathematica functions all have names beginning
with either a $ sign or else an upper case letter -- thus
"ZeroMatrix" NOT "zeros"; "For" and NOT "for"; "Length" and NOT
"length"
. In Mathematia "For" is just a function and NOT a special
syntactical construct, so it does NOT use any matching "end".
Even allowing for the completely wrong syntax, are you sure the
algorithm you list is what you really want? Your matrix E is 4-by-2,
whereas the nested for loops only produce 4 elements of E. Did you
deliberately want the last two rows (or columns??) of E to be zeros?
If indeed you wanted your 4-by-2 matrix and insist upon an elaborated
element-by-element creation of its entries, then in Mathematica you
could do it as follows:
Needs["LinearAlgebra`MatrixManipulation`"]
A={1,4}; B={2,5};
eMat=ZeroMatrix[4,2];
Do[eMat[[i,j]]=A[[i]]B[[j]],{j,1,Length[A]},{j,1,Length[B]}]
eMat
{{2,5},{8,20},{0,0},{0,0}}
Then to display that result as a 4-by-2 matrix rather than as a list of
4 2-element lists, use the function MatrixForm:
MatrixForm[eMat]
or
eMath // MatrixForm
If what you REALLY wanted was a 2-by-2 matrix, then in Mathematica you
don't need any explicit element-by-element computation at all but could
proceed as follows:
A = {1, 4}; B = {2, 5};
eMat = Outer[Times, A, B]
{{2,5},{8,20}}
eMat // MatrixForm
Even if you really did want the 4-by-2 matrix your "zeros(4,2)"
suggests, still there is no need to use looping with element-by-element
computation. Instead:
Needs["LinearAlgebra`MatrixManipulation`"]
A = {1, 4}; B = {2, 5};
eMat = ZeroMatrix[4,2];
eMat[[ {1,2},all ]] = Outer[Times,A,B];
eMat
If you're going to accomplish anything with Mathematica, it's definitely
worthwhile to begin reading the documentation supplied with it. If
you're going to use Mathematica to work with matrices, then you want to
read, among other things, the parts of the documentation dealing with lists.
Liz wrote:
> Hello, how to output the result "E" as matrix? Is there better way to get the results and output the results into matrix?
>
> Thanks in advance.
>
>
>
> A=[1 4];
> B=[2 5];
> E=zeros(4,2);
> for m=1:length(A)
> for n=1:length(B)
> E=[A(m) B(n)]
> end
> end
>
>
--
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
- References:
- Matrix Output
- From: Liz <xmzwl@yahoo.com>
- Matrix Output