meshgrid

A=[1,2,3,4]

\displaystyle \text{A}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]

B=[7;8;9]

\displaystyle \text{B}=\left[ {\begin{array}{*{20}{c}} 7 \\ 8 \\ 9 \end{array}} \right]

A (m1=1 by n1=4)

B (m2=3 by n1=1)

This gives a dimension mismatch meaning A*B is not allowed…

[A2,B2]=meshgrid(A,B)

\displaystyle \text{A2}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \end{array}} \right]

\displaystyle \text{B2}=\left[ {\begin{array}{*{20}{c}} 7 & 7 & 7 & 7 \\ 8 & 8 & 8 & 8 \\ 9 & 9 & 9 & 9 \end{array}} \right]

Both A2 and B2 have the dimensions of m2 by n1

A matrix can be calculated by using element by element multiplication:

C2=A2.*B2

\displaystyle \text{C2}=\left[ {\begin{array}{*{20}{c}} 7 & {14} & {21} & {28} \\ 8 & {16} & {24} & {32} \\ 9 & {18} & {27} & {36} \end{array}} \right]

figure(1)

contour(C2)

colorbar

figure(2)

surfc(C2)

colorbar

A (m1=1 by n1=4)

A=[1,2,3,4]

\displaystyle \text{A}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \end{array}} \right]

B (m2=3 by n1=1)

B=[7;8;9]

\displaystyle \text{B}=\left[ {\begin{array}{*{20}{c}} 7 \\ 8 \\ 9 \end{array}} \right]

A'

\displaystyle \text{A}'=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right]

B'

\displaystyle \text{B}'=\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \end{array}} \right]

A' (4 by 1)

B' (1 by 3)

Array Multiplication A'*B' is allowed as the dimensions match

\displaystyle \text{C1}=\left[ {\begin{array}{*{20}{c}} 7 & 8 & 9 \\ {14} & {16} & {18} \\ {21} & {24} & {27} \\ {28} & {32} & {36} \end{array}} \right]

C2=C1'

\displaystyle \text{C2}=\left[ {\begin{array}{*{20}{c}} 7 & {14} & {21} & {28} \\ 8 & {16} & {24} & {32} \\ 9 & {18} & {27} & {36} \end{array}} \right]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.