Problem with while loop

2 visualizaciones (últimos 30 días)
Jihye Jung
Jihye Jung el 14 de Oct. de 2019
Editada: Jihye Jung el 14 de Oct. de 2019
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!

Respuestas (1)

Fabio Freschi
Fabio Freschi el 14 de Oct. de 2019
% 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,:);
  1 comentario
Jihye Jung
Jihye Jung el 14 de Oct. de 2019
Editada: Jihye Jung el 14 de Oct. de 2019
Thank you for your answer!
But I need to use S1 and S2 data itself (without changing data) and only delete certain rows if they have big differences between each other.
For example,
S1 S2
1. 63, 246 28, 274
2. 63, 282 29, 312
3. 65, 317 33, 351
4. 68, 353 63, 119 -> S2(4,2)-S1(4,2) < -100, delete S1(4,:)
5. 100, 102 63, 157
6. 97, 137 63, 196
After delete,
S1 S2
1. 63, 246 28, 274
2. 63, 282 29, 312
3. 65, 317 33, 351
4. 100, 102 63, 119 -> Do calculate S2(4,2)-S1(4,2) < 100, go on to 5, 6
5. 97, 137 63, 157 ...until they have same size.
6. S1(7,:) 63, 196

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by