【问题标题】:Why is this index exceeding matrix dimensions?为什么这个索引超过矩阵维度?
【发布时间】:2015-01-06 15:23:57
【问题描述】:

我在调整一些 matlab 代码时遇到了麻烦,它只是绘制了一些我根据一些参数接受或拒绝的数据,然后编译接受的数据集。下面的代码是我正在调整的,但是它被设置为以行的形式读取数据。我将其更改为使用列格式的数据,但在最后的 for 循环之后一直遇到 Index exceeds matrix dimensions 错误,据我所知,该错误应该在第 10 次出现后终止。作为菜鸟,任何指导将不胜感激!

代码如下:

test = rand(3,5);           % generate 5 rows of random data
accept = 0;
reject = 0;
indices = 1:size(test,2);    % initialize indices of data based on columns
figure;


for ii = 1:size(test,2)
    plot(test(:,ii));
    [x,y,button] = ginput(1);
    if button == 97 % A - for accept
        disp('Input Accepted!');
        accept = accept + 1;
    elseif button == 114 % R - for reject
        indices(ii) = 0;
       disp('Input Rejected!');
       reject = reject + 1;
    else
        disp('Button not recongnized!')
    end
    size(test,2)

end
accept  % Display number of accepted
reject  % Displey number of rejected
indices = indices(indices~=0); % Remove indices that were rejected
new_test = test(indices,:); % Create new dataset with only accepted data

【问题讨论】:

    标签: matlab matrix indexing


    【解决方案1】:

    最后一行的命令导致了这个问题。 indices 负责 cloumns。

    用这个替换它:

    new_test = test(:, indices);
    

    我的建议是,当您的行命令多于几行时,不要使用命令窗口。您应该创建一个新脚本并将代码写入 MATLAB 编辑器。这会告诉您在使用脚本时哪一行导致了问题,因此调试会变得更容易。

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多