Trying to split a Matrix into two matrices dependant on the value of row elements.
Show older comments
Hi there,
Ultimately I am trying to split Matrix A into two matrices. One Matrix containing the rows of A with the number 55 and one containing the rows of Matrix A without the number 55.
The below script is how far I have got. This is my first attempt at just trying to delete the rows containing the value 55.
Additionally I don't know how I would even form two new matrices from the sub-divided A?
>> A=[1 2 3 55 5 ;55 1 2 55 3 ;1 2 3 4 5 ;1 2 3 4 5 ;1 55 3 4 5 ;1 2 3 4 5];
>> for m=1:length(A(:,1))
for n=1:length(A(1,:))
if A(m,n)==55
A(m,:)=[];
end
end
end
Index in position 1 exceeds array bounds (must not exceed 4).
>> A
A =
55 1 2 55 3
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
I don't understand why this script it not removing the row with two elements with value = 55. And the error message!
Any help will be gratefully received
Kind Regards
2 Comments
Bruno Luong
on 22 Sep 2018
Edited: Bruno Luong
on 22 Sep 2018
Classical logical error of someone who start programming: When you delete a row the matrix shrinks but you still loop until the last row of the original.
If you make the outer loop backward it would be OK.
There is another error more subtle in your code. Let you figure out what.
Fraser Proctor
on 22 Sep 2018
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!