Index exceeds matrix dimentions

4 ビュー (過去 30 日間)
pamela sulis
pamela sulis 2015 年 10 月 27 日
コメント済み: pamela sulis 2015 年 10 月 27 日
My question might be simple for most of you. Basically, I would like to delete a row of a matrix where a value of the elements in columns from 2 to 7 is zero. In doing so, I run the following command.
for i=1:size(StayCell,1)
if StayCell(i,2:7)==0
StayCell(i,:)=[];
end
end
However, the error message comes up saying that "Index exceeds matrix dimensions.". I don't understand why it exceeds the dimension because I have already specified that i = 1 to the length of the matrix. Any help on this would be appreciated. Thank you.
I try also this codes but i have the same problems
for i=length(StayCell)
if(StayCell(:,2:7)==0)
StayCell(i,:)=[];
end
end
StayCell(StayCell(:,2:7)==0,:)=[];
  3 件のコメント
Eng. Fredius Magige
Eng. Fredius Magige 2015 年 10 月 27 日
編集済み: Eng. Fredius Magige 2015 年 10 月 27 日
Hi try this if(~all(StayCell(:,2:7))) instead of if(StayCell(:,2:7)==0)
pamela sulis
pamela sulis 2015 年 10 月 27 日
Thanks for your suggest but using if(~all(StayCell(:,2:7))) instead of if(StayCell(:,2:7)==0), matlab generates the same error 'Index of element to remove exceeds matrix dimensions.'

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 10 月 27 日
編集済み: Stephen23 2015 年 10 月 27 日
The Problem
That error message tells us that you are trying to access a non-existent row of a matrix. Your code deletes rows of the matrix, and then tries to access rows of the original matrix size, but you do not take into account that that matrix has changed size since the start of the loop.
Lets have a look at an example script that does that same as your code:
M = [1,2;3,4;5,6]
for k = 1:size(M,1)
k % row to be removed
M(k,:) = [] % remove row
end
I did not put semi-colons on the lines, so we can see what happens as the loop iterates. This is what it displays in the command window:
M =
1 2
3 4
5 6
k =
1
M =
3 4
5 6
k =
2
M =
3 4
k =
3
Index of element to remove exceeds matrix dimensions.
Error in temp (line 6)
M(k,:) = [] % remove row
We can see the matrix is getting smaller on each iteration: first three rows, then two, then one. On the iteration with the error the matrix has just one row, but the code tries to access the third row, because k==3.
The loop does not know when you matrix has changed size, and so you are telling it to access every row of the original size of the matrix, not the size that it has after rows have been removed from it.
The Solution
Avoid ugly loops and learn to use MATLAB efficiently: different types of indexing and lots of code vectorization are your main tools to neat, fast, and efficient MATLAB code. You will likely need to use all or any too:
>> N = [1,2,3;4,0,0;0,5,0;0,0,0;6,7,0;8,9,10]
N =
1 2 3
4 0 0
0 5 0
0 0 0
6 7 0
8 9 10
>> X = all(N(:,2:3)==0,2) % rows where column 2 & 3 are both zero
X =
0
1
0
1
0
0
>> N(X,:) = [] % use X as logical index
N =
1 2 3
0 5 0
6 7 0
8 9 10
  5 件のコメント
Stephen23
Stephen23 2015 年 10 月 27 日
I am glad to hear that. How did you solve it?
pamela sulis
pamela sulis 2015 年 10 月 27 日
I use your suggest:
StayCell2=all(StayCell(:,2:7)==0,2); StayCell(StayCell2,:)=[];
I have also read the sections 'different types of indexing' and 'lots of code vectorization': they are very useful. Thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by