我是八度音阶的初学者。我创建了一个名为x的5x5矩阵,如下所示
X=ones(5,5)
我想用(-3,+ 5)替换最后一列(第5列)中的所有内容但是当我给出以下命令时它会出错
y(:,5)=(-3,+5)
我该如何纠正?
答案 0 :(得分:1)
数值数组不能将其他数组存储为元素。您必须使用单元格才能实现目标:
X = num2cell(ones(5));
X(:,5) = {[-3 5]}
X =
{
[1,1] = 1
[2,1] = 1
[3,1] = 1
[4,1] = 1
[5,1] = 1
[1,2] = 1
[2,2] = 1
[3,2] = 1
[4,2] = 1
[5,2] = 1
[1,3] = 1
[2,3] = 1
[3,3] = 1
[4,3] = 1
[5,3] = 1
[1,4] = 1
[2,4] = 1
[3,4] = 1
[4,4] = 1
[5,4] = 1
[1,5] = -3 5
[2,5] = -3 5
[3,5] = -3 5
[4,5] = -3 5
[5,5] = -3 5
}
或者,您可以通过连接单元格数组使用两个单独的步骤来构建它:
X = [num2cell(ones(5,4)) repmat({[-3 5]},5,1)]
修改强>
按照评论中的详细信息:
X = randi([0 1],5);
idx = X(:,5) == 1; % build an indexer to the elements equal to 1 in column 5
X = num2cell(X); % convert the matrix into a cell array
X(idx,5) = {[-3 5]}; % replace the indexed elements with a vector