필터 지우기
필터 지우기

Need help changing this code

조회 수: 1 (최근 30일)
Sha S
Sha S 2015년 7월 15일
답변: Walter Roberson 2015년 7월 15일
Hi, I have... a = [ 2 5 1; 3 6 2; 3 4 1; 9 4 2; 8 3 1; 3 2 2; 9 5 2; 4 8 1]
Notice how the last column follows a pattern of 1, 2,1,2..and so on. The 7th row however has a 2 in the last column just like the 6th row before...thus does not follow the pattern.
I have a code right now that deletes the 7th row, but now I want to change this code to make it delete the 6th row rather than the 7th. Can someone help me change the code to delete the 7th row instead of the 6th.
[m,n] = size(a);
expected = 1; % initialize expected value in 1st row
x = false(m,1); % initialize the deletion flag array
for k=1:m
if( a(k,n) ~= expected )
x(k) = true; % if not as expected, mark for deletion
else
expected = 3 - expected; % if as expected, update expected
end
end
a(x,:) = []; % delete the unexpected pattern rows

답변 (1개)

Walter Roberson
Walter Roberson 2015년 7월 15일
The obvious change would be
x(k-1) = true;
but then you face the question of what you want to delete if the first entry, k = 1, was not as expected

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by