Math & Science"Advanced" matrices

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
x42bn6
Posts: 14
Joined: Tue Apr 07, 2009 1:35 am

"Advanced" matrices

Post by x42bn6 »

Does anyone know how to do matrices but a bit more?

Of course it's a little bit hard without any code, but consider the matrix:

Code: Select all

1 2 2 2 2 2
2 1 2 2 2 2
2 2 1 2 2 2
2 2 2 1 2 2
2 2 2 2 1 2
2 2 2 2 2 1
I'd like to be able to do something like put 1s on the diagonal and then put a big "2" in the top-right and bottom-left parts of the matrix.

Anyone have any ideas?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

"Advanced" matrices

Post by Stefan Kottwitz »

Hi,

that's no problem even without any package:

Code: Select all

\documentclass[a4paper,10pt]{article}
\begin{document}
\[
\left(
  \begin{array}{cccccc}
    1 & \\
      & 1 & & & \mbox{\Huge $2$}\\
      & & 1 & \\
      & & & 1 & \\
      & \mbox{\Huge $2$} & & & 1 \\
      & & & & & 1
  \end{array}
\right)
\]
\end{document}
There are packages like easybmat and blkmatrix providing support for block matrices.

Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

"Advanced" matrices

Post by localghost »

An alternative to Stefan's approach.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{multirow}

\begin{document}
  \[
    \begin{pmatrix}
      1 & & & & \multirow{3}{*}{\Huge 2} & \\
      & 1 & & & & \\
      & & 1 & & & \\
      & \multirow{3}{*}{\Huge 2} & & 1 & & \\
      & & & & 1 & \\
      & & & & & 1
    \end{pmatrix}
  \]
\end{document}
For more information refer to the manuals of the used packages.

The »Math mode« document might give some more useful hints to handle that. Stefan already mentioned a package from the easy bundle which provides several packages for mathematical expressions.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

"Advanced" matrices

Post by lalop »

Don't know of any packages. I would just make a newcommand using a counter for the number of times you've used it:

Code: Select all

\usepackage{calc}
\usepackage{ifthen}
%
\newcounter{rownumber}
\newcounter{columnnumber}

% Filldiagonal{diagonalvalue}{other value}{element counter}{row size}
%check if diagonal, puts as diagonalvalue, otherwise other value
\newcommand{\filldiagonal}[4]{%
%
% Much easier to think of the rows and columns starting at 0,
%then the modulus gives column number, integer divison gives row number
%
\setcounter{rownumber}{\value{#3}/#4}%
\setcounter{columnnumber}{\value{#3}-\value{rownumber}*#4}%
\ifthenelse{\value{columnnumber}=\value{rownumber}}{#1}{#2}%
\stepcounter{#3}%
%
}
You'd still have to copy and paste the function into every element, but at least it's easier.

Code: Select all

\newcounter{eleme}
\setcounter{eleme}{0}

\begin{array}{cccccccccc}
\filldiagonal{1}{2}{eleme}{10} & ... & \filldiagonal{1}{2}{eleme}{10}\\
\filldiagonal{1}{2}{eleme}{10}  & ... & \filldiagonal{1}{2}{eleme}{10}\\
...
\filldiagonal{1}{2}{eleme}{10} } & ... & \filldiagonal{1}{2}{eleme}{10}\\
\end{array}
And you can make similar macros for other matricies. The disadvantage is that it can take a lot of time to figure out the row, column # if you're not careful.
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

"Advanced" matrices

Post by lalop »

Found an even easier way, using for loops. Here's a complete document:

Code: Select all

\documentclass[11pt]{memoir}
\usepackage{ifthen}
\usepackage{forloop} 


\newcommand{\filldiagonal}[5]{
\ifthenelse{#3=#4}{#1}{#2}%
\ifthenelse{#4<#5}{ & }{\\}%
}


\begin{document}

\newcounter{rownumber}
\newcounter{columnnumber}


$$\begin{array}{cccccccccc}
\forLoop{1}{10}{rownumber}{%
	\forLoop{1}{10}{columnnumber}{%
		\filldiagonal{1}{2}{\value{rownumber}}{\value{columnnumber}}{10}%
	}%
}\end{array}$$

\end{document} 

(The artifact at the top right is just the page number.)

The weird bit, though, is that it won't let me insert the contents of the filldiagonal directly into the for loop. E.g.

Code: Select all

\documentclass[11pt]{memoir}
\usepackage{ifthen}
\usepackage{forloop} 


\newcommand{\filldiagonal}[5]{
\ifthenelse{#3=#4}{#1}{#2}%
\ifthenelse{#4<#5}{ & }{\\}%
}


\begin{document}

\newcounter{rownumber}
\newcounter{columnnumber}


$$\begin{array}{cccccccccc}
\forLoop{1}{10}{rownumber}{%
	\forLoop{1}{10}{columnnumber}{%
		\ifthenelse{\value{rownumber} = \value{columnumber}}{1}{2}%
		\ifthenelse{\value{columnnumber}<10}{\\}{&}%	
	}
}

\end{array}$$

\end{document}
Gives errors saying "You can't use \relax after the }"
mdk31
Posts: 20
Joined: Wed Mar 11, 2009 4:38 am

"Advanced" matrices

Post by mdk31 »

localghost wrote:An alternative to Stefan's approach.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{multirow}

\begin{document}
  \[
    \begin{pmatrix}
      1 & & & & \multirow{3}{*}{\Huge 2} & \\
      & 1 & & & & \\
      & & 1 & & & \\
      & \multirow{3}{*}{\Huge 2} & & 1 & & \\
      & & & & 1 & \\
      & & & & & 1
    \end{pmatrix}
  \]
\end{document}
For more information refer to the manuals of the used packages.

The »Math mode« document might give some more useful hints to handle that. Stefan already mentioned a package from the easy bundle which provides several packages for mathematical expressions.
I think the \multirow command is having problems running in my math environment.

Code: Select all

\documentclass[twoside,12pt,openright]{report}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{array}
\usepackage{multirow}
\begin{equation}
\text{adj}({\bf A}^T \! {\bf A})_{k,k}= \left|
\begin{bmatrix}
u^1_a & ... & u^m_a \\
u^1_b & ... & u^m_b \\
\lambda & ... & \lambda
\end{bmatrix}
\begin{bmatrix}
\multirow{2}{*}{\large {\bf R}_r} & u^m_a \\ 
& u^m_b \\
0^T & \lambda
\end{bmatrix} \right|
\end{equation}
\end{document}
I'm getting errors like "Missing $ inserted" or "Missing } inserted" which I think is because of the \multirow in the equation environment.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

"Advanced" matrices

Post by localghost »

mdk31 wrote:I think the \multirow command is having problems running in my math environment. [...] I'm getting errors like "Missing $ inserted" or "Missing } inserted" which I think is because of the \multirow in the equation environment.
You are wrong. Obviously you haven't tested my example. Otherwise you would have found out that it's working fine. The only thing that doesn't work is your way of choosing font styles for boldface in math mode.


Best regards
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
mdk31
Posts: 20
Joined: Wed Mar 11, 2009 4:38 am

"Advanced" matrices

Post by mdk31 »

Well, I'm not sure why that is because now it's telling me that I'm not in math mode so I can't use \mathbf.

Code: Select all

\begin{equation}
\text{adj}({\bf A}^T \! {\bf A})_{k,k}= \left|
\begin{bmatrix}
u^1_a & ... & u^m_a \\
u^1_b & ... & u^m_b \\
\lambda & ... & \lambda
\end{bmatrix}
\begin{bmatrix}
\multirow{2}{*}{\large \mathbf{R}_k} & u^m_a \\ 
& u^m_b \\
0^T & \lambda
\end{bmatrix} \right|
\end{equation}
\end{document}
mdk31
Posts: 20
Joined: Wed Mar 11, 2009 4:38 am

"Advanced" matrices

Post by mdk31 »

Okay, I think I see now how to get rid of the error. But I'm still having problems with the format because I know that you can't use multirow for the second matrix. I need a command that will stretch the R_k across the second column in the first row.

Code: Select all

\documentclass[twoside,12pt,openright]{report} 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{array}
\usepackage{multirow}
\setlength{\extrarowheight}{0.4cm}
\newcommand{\vectornorm}[1]{\left|\left|\mathbf{#1}\right|\right|}
\newcommand{\mb}[1]{\mathbf{#1}}
\begin{document}
\begin{equation}
\text{adj}(\mb{A}^T \! \mb{A})_{k,k}= \left|
\begin{bmatrix}
\multirow{2}{*}{$\mb{R}_k$} & u^m_a \\ 
& u^m_b \\
0^T & \lambda
\end{bmatrix}
\begin{bmatrix}
\multirow{2}{*}{$\mb{R}^T_k$} &  & 0 \\
u^m_a & u^m_b & \lambda \\
\end{bmatrix}\right|
\end{equation}
\end{document}
Post Reply