将元素添加到预定义的数组中

时间:2013-12-19 02:36:40

标签: matlab octave

这是我创建的函数的一段代码,它给出了一个在网格中找到的单词,并在网格中找到了单词。我在这里要做的是通过从起点(行和列等于)开始在给定方向上获得单词。 rowdircoldir依赖于switch语句,如果方向是东北方向,例如rowdir=-1coldir=1。我想输出单词然后在网格上绘制它。

word = zeros(1,len);
for index = 1:len
    index_1 = index-1;
    word(index) = grid1(row + (index_1 * rowdir), column + (index_1 * coldir));
end

我收到此错误并且不确定如何解决它:

In an assignment  A(I) = B, the number of elements in B and I must be the
same.

Error in jifjffj (line 43)
        word(index) = grid1(row + (index_1 * rowdir), column + (index_1 *
        coldir));

如果我在命令提示符中输入相同的代码,其中行和列设置为5,并且为特定方向调整了rowdir和coldir并给定了矩阵,则在该方向上提取单词时没有问题指定的字母长度,所以我不确定这里的问题是什么。


(来自其他发布信息的编辑):

我还尝试了以下操作,这会导致相同的错误消息。我很难理解为什么。

testword= [];
for index = 1:len
    index_1 = index-1;
    word = grid(row + (index_1 * rowdir), column + (index_1 * coldir));
    testword(end+1) = word;
end

1 个答案:

答案 0 :(得分:1)

我猜你的rowcolumn不是一个数字。所以你的

grid1(row + (index_1 * rowdir), column + (index_1 * ...
        coldir))

实际上是一个2D数组,而word(len)只是一个数字。

试试这个:

for index = 1:len
    index_1 = index-1;
    word = grid(row + (index_1 * rowdir), column + (index_1 * coldir));
    testword{index} = word;
end
相关问题