While Loop Keep running non stop !!

i have a matrix A
A=[ 1 1 2
3 3 4
4 4 5
5 5 6
6 6 7
7 7 8
8 8 9
9 9 10
10 10 11
12 12 13
13 13 14
15 15 16
16 16 17
17 17 18
18 2 19
19 19 20
20 20 21
21 21 22
22 3 23
23 23 24
24 24 25
26 26 27
27 27 28
28 28 29
28 29 30
30 30 31
31 31 32
33 21 8
34 9 15
35 22 12
36 18 33
37 25 29]
i want to make a loop which detect repeated values of column no. 3 then swap the repeated value of column 3 with column 2 from bottom to up and keep swapping going up until no repeated values in column 3
i made somthing like that
[c,ia,ib] = unique(A(:,3));
FF=length(ia)-1;
Ncount = histc(A(:,3), c);
for i=FF:-1:1
Ncount = histc(A(:,3), c);
while Ncount(i)==2
A(i,[2,3])=A(i,[3,2]);
Ncount = histc(A(:,3), c);
end
end
but the loop keep running and i think that because of two Ncount=2 at row 28 (29 is repeated in row 37 and 28) and Ncount=2 at row 7 (8 is repeated at row 7 and 33) !!
please help !

4 Comments

What do you expect the matrix "A" to look like after the replacements are complete (just to make sure the problem is understood)?
excpected "A" to look like this
A=[ 1 1 2
3 4 3
4 5 4
5 6 5
6 7 6
7 8 7
8 8 9
9 9 10
10 10 11
12 12 13
13 13 14
15 15 16
16 16 17
17 17 18
18 2 19
19 19 20
20 20 21
21 21 22
22 3 23
23 23 24
24 24 25
26 27 26
27 28 27
28 29 28
29 29 30
30 30 31
31 31 32
33 21 8
34 9 15
35 22 12
36 18 33
37 25 29];
(notice: the number of rows on in column 1 missing some numbers so any row number am mentioning is according to row numbers found in column 1)
as you can see row 28 (the 29 was swapped with 28) which result in row 27 (the 28 was swapped with 27) so no repeated value at column 3.. and so on till no repeated value, then it goes up to row 8 and swapped 8 with 7 since number 8 is repeated (found in row 33) and then keeps swapping moving up till no repeated values in column 3.
i think the main problem here that Ncount give two index of repeated values which is [6 & 26] the index 6 is right since it points to number 8 but index 26 is not pointing to 29 which supposed to be pointing at.
Your loop doesn't exit because you never meet the break conditions. When I ran it I got that Ncount was a 1x10 array that never changed, while the loop was looking for element 26 of N to be equal to 2.
yes thats the problem and i cant fix it.. i dont even know if using while loop is a correct choice !!

Sign in to comment.

 Accepted Answer

Bob Thompson
Bob Thompson on 22 Feb 2019
Edited: Bob Thompson on 22 Feb 2019
Ok, I think I got something working. The while loop was a fine choice, it was more a matter of indexing and changing things together. Also, it was impossible to start changing values from the back, because you ran into an infinite loop.
[c,ia,ib] = unique(A(:,3));
Ncount = histc(A(:,3), c);
if sum(Ncount >= 2) >0;
c2 = c(Ncount >= 2);
while sum(Ncount >= 2) > 0
A(find(ismember(A(:,3),c2),max(Ncount),'first'),[3,2]) = A(find(ismember(A(:,3),c2),max(Ncount),'first'),[2,3]);
[c,ia,ib] = unique(A(:,3));
Ncount = histc(A(:,3), c);
c2 = c(Ncount >= 2);
end
end

3 Comments

it worked for the matrix A i sent, but when ,matrix A changed it Looped without ending
the new matrix was
A=[ 1 1 2
2 2 3
3 3 4
5 5 6
6 6 7
7 7 8
8 8 9
9 9 10
11 11 12
13 13 14
14 14 15
15 15 16
16 16 17
17 17 18
18 2 19
19 19 20
20 20 21
21 21 22
22 3 23
23 23 24
25 6 26
26 26 27
27 27 28
28 28 29
28 29 30
31 31 32
32 32 33
33 21 8
34 9 15
35 22 12
36 18 33
37 25 29];
and it looped without stopping !!
i want it to be dynamic with any matrix of this kind even if there is more than two duplicate elements, please if you could !
Bob Thompson
Bob Thompson on 22 Feb 2019
Edited: Bob Thompson on 22 Feb 2019
[c,ia,ib] = unique(A(:,3));
Ncount = histc(A(:,3), c);
if sum(Ncount >= 2) >0;
c2 = c(Ncount >= 2);
while sum(Ncount >= 2) > 0
for i = 1:size(A,1);
if ~ismember(A(i,2),c)
A(i,[3,2]) = A(i,[2,3]);
end
[c,ia,ib] = unique(A(:,3));
end
Ncount = histc(A(:,3), c);
c2 = c(Ncount >= 2);
end
end
I think this one is a bit more robust. Let me know.
EDIT**
For the record, the while loop should now be obsolete. When I ran it the loop only occured once.
Man your the best .. it worked, thank you so mcuh.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!