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 Kommentare

Stephen23
Stephen23 am 27 Okt. 2015
Bearbeitet: Stephen23 am 27 Okt. 2015
@pamela sulis: please stop posting duplicate questions. So far you have posted the same basic question at least three times. Doing so actually makes our work harder as to keep track of what you are writing and what answers you have been given. I closed the other questions.
Instead of creating duplicates you can simply edit your question or add a comment to it (note: do not add an answer unless it really is an answer).
Eng. Fredius Magige
Eng. Fredius Magige am 27 Okt. 2015
Bearbeitet: Eng. Fredius Magige am 27 Okt. 2015
Hi try this if(~all(StayCell(:,2:7))) instead of if(StayCell(:,2:7)==0)
pamela sulis
pamela sulis am 27 Okt. 2015
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.'

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 27 Okt. 2015
Bearbeitet: Stephen23 am 27 Okt. 2015

1 Stimme

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 Kommentare

pamela sulis
pamela sulis am 27 Okt. 2015
Sorry for my duplicate questions: it's the first time I use this forum and I don't know the rules.
About my question: I understand the problem but I can't find a solution... I try different method but i generate the same error or a new error about matrix dimensions
Stephen23
Stephen23 am 27 Okt. 2015
Bearbeitet: Stephen23 am 27 Okt. 2015
I just gave you the neatest and fastest solution using indexing and no loops, together with a full working example and links to the documentation. Using logical indexing is the easiest way to achieve your aim.
You should be able to adapt my example: I told you that it checks columns two and three, you just need to adapt this for the columns that you need to check. And you need to clarify if the condition is any zero in those columns, or only zeros in those columns.
pamela sulis
pamela sulis am 27 Okt. 2015
Thanks, I have solved the problem!
Stephen23
Stephen23 am 27 Okt. 2015
I am glad to hear that. How did you solve it?
pamela sulis
pamela sulis am 27 Okt. 2015
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by