打印矩阵和矢量并排 [英] printing matrices and vectors side by side

查看:173
本文介绍了打印矩阵和矢量并排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了教程的目的,我希望能够并排打印或显示矩阵和向量,通常是为了说明矩阵方程的结果,如$ A x = b $。



我可以使用SAS / IML来执行此操作,其中 print 语句采用任意的(空格分隔的)表达式集合,对它们进行求值并打印结果,例如,

  print A'*'x'='(A * x)'='b; 

AX#TEM1001 B
1 1 -4 * 0.733 = 2 = 2
1 -2 1 -0.33 1 1
1 1 1 -0.4 0 0

请注意,带引号的字符串是按原样打印的。

<我已经搜索了,但是在R中却找不到这样的东西。我想象这样的事情可以通过一个函数 showObj(object,...)来完成,参数列表,将每个格式化为一个字符块,并且将它们并排放在一起。


另一个用法是显示3D数组作为它的切片的并排集合。

这是一个响铃还是没有人有建议吗?

解决方案

我创建了一个非常简单的函数,可以打印具有任意字符的矩阵和向量字符串(通常是运算符)之间。它允许具有不同行数的矩阵,并将向量当作列矩阵。这不是很详细,所以我担心有很多例子失败。但是对于一个简单的例子来说,应该就足够了。

format()是用于将数字转换为字符。这具有如下优点:矩阵的所有行具有相同的宽度,因此在打印时很好地对齐。如果需要的话,你可以添加一些参数 format()作为参数 mat_op_print()来使可配置。作为一个例子,我添加了可以用来控制列的最小宽度的参数 width



如果函数调用中的矩阵和向量是名称,则这些名称将作为标题打印在第一行中。所以,这是一个函数:

 <$ 

c $ c> mat_op_print< - function(...,width = 0){

#获取参数
args< - list(...)
chars< ; - sapply(args,is.character)

#辅助函数创建n个空格的字符
空格< - 函数(n)粘贴(rep(,n) =)

#将向量转换为行矩阵
vecs< - sapply(args,is.vector)
args [vecs& (chars)< - lapply(args [vecs&!chars],function(v)matrix(v,ncol = 1))

#将所有非字符转换为格式为$ b的字符$ b $ args [!chars]< - lapply(args [!chars],format,width = width)

#打印名字作为第一行(如果存在)
arg_names<如果(is.matrix(x)){$ b($ name))
if(!is.null(arg_names)){
get_title< - function(x,name){

(其他){
(nchar(x [1,]))+ ncol(x) - 1 - nchar(name))


$ b cat(mapply(get_title,args,arg_names),\\\

}

#辅助函数
get_line < - function(x,n){
if(is.matrix(x)){
if(nrow(x) (sum(nchar(x [1,]))+ ncol(x) - 1)
} else {
paste(x [n,],collapse =)
}
} else if(n == 1){
x
} else {
spa ces(nchar(x))
}
}

#根据需要为行打印多行
N <-max(sapply( args [!chars],nrow))
for(n in 1:N){
cat(sapply(args,get_line,n),\\\

}



$ b $ p
$ b

这是一个如何工作的例子:

$ $ p $ code> A =矩阵(c(0.5,1,3,0.75,2.8,4),nrow = 2)
x = c(0.5,3.7, 2.3)
y = c(0.7,-1.2)
b = A%*%x - y
mat_op_print(A = A,*,x = x, - ,y = y,=,b = b,宽度= 6)
## A xyb
## 0.50 3.00 2.80 * 0.5 - 0.7 = 17.090
## 1.00 0.75 4.00 3.7 -1.2 13.675
## 2.3

同时打印一个三维数组的切片,

pre $ A< - array(1:12,dim = c(2,2,3))
mat_op_print(A1 = A [,,1],| ,A2 = A [,,2],| ,A3 = A [,,3])
## A1 A2 A3
## 1 3 | 5 7 | 9 11
## 2 4 6 8 10 12


For tutorial purposes, I'd like to be able to print or display matrices and vectors side-by-side, often to illustrate the result of a matrix equation, like $A x = b$.

I could do this using SAS/IML, where the print statement takes an arbitrary collection of (space separated) expressions, evaluates them and prints the result, e.g.,

print A ' * ' x '=' (A * x) '=' b;

                    A                     X   #TEM1001       B
                    1     1    -4  *  0.733 =        2 =     2
                    1    -2     1     -0.33          1       1
                    1     1     1      -0.4          0       0

Note that quoted strings are printed as is.

I've searched, but can find nothing like this in R. I imagine something like this could be done by a function showObj(object, ...) taking its list of arguments, formatting each to a block of characters, and joining them side-by-side.

Another use of this would be a compact way of displaying a 3D array as the side-by-side collection of its slices.

Does this ring a bell or does anyone have a suggestion for getting started?

解决方案

I have created a very simple function that can print matrices and vectors with arbitrary character strings (typically operators) in between. It allows for matrices with different numbers of rows and treats vectors as column matrices. It is not very elaborate, so I fear there are many examples where it fails. But for an example as simple as the one in your question, it should be enough.

format() is used to convert the numbers to characters. This has the advantage that all the rows of the matrix have the same width and are thus nicely aligned when printed. If needed, you could add some of the arguments of format() also as arguments mat_op_print() to make the configurable. As an example, I have added the argument width that can be used to control the minimal width of the columns.

If the matrices and vectors are name in the function call, these names are printed as headers in the first line. Otherwise, only the numbers are printed.

So, this is the function:

mat_op_print <- function(..., width = 0) {

  # get arguments
  args <- list(...)
  chars <- sapply(args, is.character)

  # auxilliary function to create character of n spaces
  spaces <- function(n) paste(rep(" ", n), collapse = "")

  # convert vectors to row matrix
  vecs <- sapply(args, is.vector)
  args[vecs & !chars] <- lapply(args[vecs & !chars], function(v) matrix(v, ncol = 1))

  # convert all non-characters to character with format
  args[!chars] <- lapply(args[!chars], format, width = width)

  # print names as the first line, if present
  arg_names <- names(args)
  if (!is.null(arg_names)) {
    get_title <- function(x, name) {
      if (is.matrix(x)) {
        paste0(name, spaces(sum(nchar(x[1, ])) + ncol(x) - 1 - nchar(name)))
      } else {
        spaces(nchar(x))
      }
    }
  cat(mapply(get_title, args, arg_names), "\n")
  }

  # auxiliary function to create the lines
  get_line <- function(x, n) {
    if (is.matrix(x)) {
      if (nrow(x) < n) {
       spaces(sum(nchar(x[1, ])) + ncol(x) - 1)
      } else {
        paste(x[n, ], collapse = " ")
      }
    } else if (n == 1) {
      x
    } else {
      spaces(nchar(x))
    }
  }

  # print as many lines as needed for the matrix with most rows
  N <- max(sapply(args[!chars], nrow))
  for (n in 1:N) {
    cat(sapply(args, get_line, n), "\n")
  }
}

And this is an example of how it works:

A = matrix(c(0.5, 1, 3, 0.75, 2.8, 4), nrow = 2)
x = c(0.5, 3.7, 2.3)
y = c(0.7, -1.2)
b = A %*% x - y
mat_op_print(A = A, " * ", x = x, " - ", y = y, " = ", b = b, width = 6)
## A                        x          y          b      
##   0.50   3.00   2.80  *     0.5  -     0.7  =  17.090 
##   1.00   0.75   4.00        3.7       -1.2     13.675 
##                             2.3    

Also printing the slices of a 3-dimensional array side-by-side is possible:

A <- array(1:12, dim = c(2, 2, 3))
mat_op_print(A1 = A[, , 1], " | ", A2 = A[, , 2], " | ", A3 = A[, , 3])
## A1      A2      A3    
## 1 3  |  5 7  |   9 11 
## 2 4     6 8     10 12

这篇关于打印矩阵和矢量并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆