While Loop Keep running non stop !!
Show older comments
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
Adam Danz
on 22 Feb 2019
What do you expect the matrix "A" to look like after the replacements are complete (just to make sure the problem is understood)?
hossam eldakroury
on 22 Feb 2019
Bob Thompson
on 22 Feb 2019
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.
hossam eldakroury
on 22 Feb 2019
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!