如何在矩阵前添加一列? [英] How to prepend a column to a matrix?

查看:424
本文介绍了如何在矩阵前添加一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,假设我有这个矩阵:{{1,2},{2,3}},而我宁愿有{{4,1,2},{5,2,3}}.也就是说,我在矩阵的前面添加了一个列.有简单的方法吗?

Ok, imagine I have this Matrix: {{1,2},{2,3}}, and I'd rather have {{4,1,2},{5,2,3}}. That is, I prepended a column to the matrix. Is there an easy way to do it?

我最好的建议是这样

PrependColumn[vector_List, matrix_List] := 
 Outer[Prepend[#1, #2] &, matrix, vector, 1]

但是它混淆了代码,并不断要求加载越来越多的代码.这不是内置的吗?

But it obfuscates the code and constantly requires loading more and more code. Isn't this built in somehow?

推荐答案

由于在Mathematica 6中引入了ArrayFlatten,所以必须使混淆最少的解决方案是

Since ArrayFlatten was introduced in Mathematica 6 the least obfuscated solution must be

matrix = {{1, 2}, {2, 3}}
vector = {{4}, {5}}

ArrayFlatten@{{vector, matrix}}

一个不错的技巧是用0替换任何矩阵块都会得到大小合适的零块.

A nice trick is that replacing any matrix block with 0 gives you a zero block of the right size.

这篇关于如何在矩阵前添加一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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