Problem with while loop
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello, I have a trouble with making code and it make me so frustrated.
I would like to ask you for the while loop which I did.
I just want to count S1 and S2 which has not big differences (absoulte value of S1-S2 <100).
I want to count until they have same size of S1 and S2 and if the delete S1(k-th) row or S2(k-th) row, then I want to go back k-th data and want to compare again.
Size of S1 = 168 x 2 and S2 = 166 x 2.
I calculated manually I found final value of S1 = 164 x 2 and S2 = 164 x 2.
% S1 has 168x2 double data and S2 has 166x2 double data.
count = 1;
for k = 1:1000 % Process until size(S1) = size(S2)
while size(S1(1)) ~= size(S2(1)) % until size(S1(1)) = size(S2(1))
% I want to get S1 = 164x2 ; S2 = 164x2;
if (S2(k,2)-S1(k,2))<-100 % If big differences then delete that k-th row
S1(k,:) = [];
elseif (S2(k,2)-S1(k,2))>100 % If big differences then delete that k-th row
S2(k,:) = [];
else (S2(k,2)-S1(k,2)) < 100 && (S2(k,2)-S1(k,2)) > -100;
% Count if they don't have big difference
S1(count,:) = S1(k);
S2(count,:) = S2(k);
count = count+1;
end
end
sc1 = S1(k,:); % Want to save final data
sc2 = S2(k,:);
end
If there have any good idea for this, please share your intelligent knowledge.
Thank you!
댓글 수: 0
답변 (1개)
Fabio Freschi
2019년 10월 14일
% make matrices of the same length
n1 = size(S1,1);
n2 = size(S2,1);
S1 = S1(min([n1,n2]),:);
S2 = S2(min([n1,n2]),:);
% now compare the two matrices (second column, according to the example) and extract the indices
idx = abs(S1(:,2)-S2(:,2)) < 100;
% keep only the desired values
S1 = S1(idx,:);
S2 = S2(idx,:);
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!