25 views (last 30 days)

Show older comments

Me about 2 hours ago

Edited: VBBV 32 minutes ago

Accepted Answer: Aquatris

Hi everyone! I need help with my code.I have a matrix (300x300) with some equal rows, and I need to eliminate the duplicate rows. I can't solve the error. Can anyone help me??Thank you very much!

Below I copy the part of the code with the error and a simplified example.

clear all

matrix_f=[1 2 3; 1 2 3; 4 5 6; 7 8 9; 7 8 9; 10 11 12];

for k=1:length(matrix_f(:,1))

if matrix_f(k,1)==matrix_f(k+1,1)

matrix_f(k+1,:)=[];

end

end

##### 1 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

VBBV 33 minutes ago

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2142101-error-in-for-loop-index-exceeds-matrix-dimensions#comment_3226581

Edited: VBBV 32 minutes ago

@Me, you need to give the column dimension for the matrix in the for loop and use size function instead of length

clear all

matrix_f=[1 2 3; 1 2 3; 4 5 6; 7 8 9; 7 8 9; 10 11 12];

for k=1:size(matrix_f,2) % give the column dimension here

if isequal(matrix_f(k,:),matrix_f(k+1,:)) % use isequal function for check

matrix_f(k,:)=[]; % delete duplicate rows of numbers

end

end

matrix_f % result

matrix_f = 4x3

1 2 3 4 5 6 7 8 9 10 11 12

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

Sign in to comment.

Sign in to answer this question.

### Accepted Answer

Aquatris 11 minutes ago

Edited: Aquatris 10 minutes ago

Open in MATLAB Online

You have essentially made two mistakes:

- matrix_f only has 6 rows. You setup up your for loop such that k is 1 2 3 4 5 6 . When k is 6, you are trying to reach the 7th row of matrix_f, which does not exist. So your for loop should iterate from 1 to [length(matrix_f(:,1)-1].
- Within the for loop you change the size of matrix_f by removing rows. If you want to do this type of manipulation, you should just store the index you want to remove and remove the rows once your for loop is done. Otherwise indexing becomes messed up and you will run into the same issue, trying to reach a row that does not exist.
- You only remove rows that are next to each other.

Of course vectorizing the code is always better as in @Torsten answer.

clear all

matrix_f=[1 2 3; 1 2 3; 4 5 6; 7 8 9; 7 8 9; 10 11 12];

idx2remove = [];

for k=1:(length(matrix_f(:,1))-1)

if matrix_f(k,1)==matrix_f(k+1,1)

idx2remove = [idx2remove;k];

end

end

matrix_f(idx2remove,:) = [];

matrix_f

matrix_f = 4x3

1 2 3 4 5 6 7 8 9 10 11 12

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

##### 1 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Me 3 minutes ago

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2142101-error-in-for-loop-index-exceeds-matrix-dimensions#comment_3226551

Thank you very much!

Sign in to comment.

### More Answers (1)

Torsten about 2 hours ago

Edited: Torsten about 2 hours ago

Open in MATLAB Online

matrix_f=[1 2 3; 1 2 3; 4 5 6; 7 8 9; 7 8 9; 10 11 12];

unique(matrix_f,'stable','rows')

ans = 4x3

1 2 3 4 5 6 7 8 9 10 11 12

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

Note that this code will remove all duplicate rows in the matrix, not only subsequent ones.

##### 1 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Me 3 minutes ago

#### Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2142101-error-in-for-loop-index-exceeds-matrix-dimensions#comment_3226546

Many thanks to you too!

Sign in to comment.

Sign in to answer this question.

### See Also

### Tags

- for
- for loop
- matrix
- index exceeds matrix dimensions.

### Community Treasure Hunt

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

Start Hunting!

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office