【问题标题】:What is 'index out of bounds' error in Octave?Octave 中的“索引越界”错误是什么?
【发布时间】:2021-11-09 11:16:42
【问题描述】:

我一直在 Octave 中过滤数据但出现错误

index out of bounds: value 741 out of bound 740'

X = 892x2 矩阵和第 2 列包含一些值 = 0。我希望删除所有值为 0 的行。

这是我的代码

train_set = csvread('train.csv');
X = [train_set(:,3),train_set(:,7)];
y = train_set(:,2);
m = size(X,1);
for i=1:m,
  if train_set(i,7) == 0,
    X(i,:)=[];
  endif
end

【问题讨论】:

  • 该错误意味着您有一个值为 741 的索引超出了大小为 740 的数组的范围。

标签: arrays matrix octave


【解决方案1】:

您正在循环期间修改 X。这意味着虽然最初它有 892 行,但在第一个零之后,您将其减少到 891 行,然后减少到 890 行,依此类推。

在某些时候,您已将其减少到 740 行;在此之后的某个时间点,循环中的i 达到数字 741。

这导致以下类型的指令:X(741, :) = []

Octave 尽职尽责地告诉你 X 不再有第 741 行了。

相反,您可以做的是标记要删除的行,并在循环后使用单个命令将其删除。或者更好的是,没有 foo 循环,你可以这样做:

Condition = train_set(:,7) == 0;   % find all rows you want to delete
X( Condition, : ) = [];   % Use logical indexing on rows to delete them.

【讨论】:

猜你喜欢
  • 2014-06-06
  • 2015-03-22
  • 2013-06-06
  • 2013-10-13
  • 2015-01-10
  • 2014-01-27
  • 1970-01-01
相关资源
最近更新 更多