Matlab向矩阵添加额外的列 [英] Matlab adding extra columns to a matrix

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

问题描述

我有一个大小为216 x 3的矩阵.我希望在此矩阵中增加三列.我以为很简单的事情使我发疯!我原以为它就像下面的线一样,

I have a matrix that is 216 x 3 in size. I wish to add an extra three columns to this matrix. Something I thought was quite simple is driving me crazy! I was expecting it to be something like the line below,

myData = [myData; zeros(length(myData(:,1)),3)]

错误消息如下.但是,两个矩阵的行数相同,因此不了解问题所在.

The error message is below. However both matrices have the same number of rows so do not understand what the problem is.

Error using vertcat
Dimensions of matrices being concatenated are not consistent.

更新

我现在在下面显示以下行,但仍然会收到与上面相同的错误消息,但对于horzcat.

I now have the following line below and I still can get an error message, same as above but for horzcat.

myData = [myData zeros(size(myData, 1),3)];

我还注意到myData实际上定义为213 x3单元格-这可能是问题吗?

I have also noticed myData is actually defined as 213 x3 cell - could this be the issue?

推荐答案

将零填充到数组中的另一种方法是使用

Another way to pad zeros into arrays would be using padarray -

N = 3; %%// Number of columns to be added
myData = padarray(myData,[0 N],'post')

响应OP的更新:

col_ind = size(myData,2)+1:size(myData,2)+3;

其中之一必须有效-myData(:,col_ind) = {'0'}myData(:,col_ind) = {0}取决于是否需要分别由字符或数字组成的单元格数组.

One of these must work - myData(:,col_ind) = {'0'} or myData(:,col_ind) = {0} depending on if a cell array of characters or numerals is needed respectively.

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

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