2

i've got an array like x, i want to do some works on it and put result in the new array y. then i should compare this two. if they are the same by a thershold(i.e they could be a little different) that's ok and algorithm ends otherwise i should continue the iteration the problem is comparing these two. they are a two 2d array with unknown elements. i've done two different way but none of them where ok: first way:

d = x - y
if d < 5 
   disp('end')
end

and so on
but it does not work well,honestly it doesn't work at all
the other way which i used is:

isequal(x,y)

while they are the same it will return 0 but if they are not and even with a little difference the result will be 1 and it is not ok cause as i said algorithm should consider a litlle difference and stop the iteration what should i do?

deansam
  • 68
  • 1
  • 2
  • 8

2 Answers2

3

If 5 is an OK threshold, then this should work:

d=abs(x-y);
if all(d<5)
    disp('end')
end

If you don't know what the threshold is, then that's a very different question. Determining a sensible threshold is dependant on your application, and is often a trade-off - there may not be a "right" answer if your data is variable. Look into some basic statistics - the zscore command may be a useful start.

Hugh Nolan
  • 2,508
  • 13
  • 15
  • it didn't work the answer array got numbers like 30 40 22 and so on but it accept d<5 – deansam Jul 16 '13 at 14:56
  • Can you show details of the array? Code for `x` and `y`? I can't help you without any more details! – Hugh Nolan Jul 16 '13 at 14:59
  • my code is too long and so complicated i dono if i put a part of it here it would be understandable,anyway the code is: k=44;%k group n=556;%all elements r=12;%elements in a group b=1;%the column number which is 1 %1st gouping data in k group.each group own a row. a is a cell array a=grouhbandi(n,k); e1=Ks(a,k); %the first elements of a in each row which are the k means,e1 is array i=0; had=1000;%limit to stop iteration while had>5 a=Ekmeans(a,k,r); a=newks(a,k);%these two code calculate the new k group e2=Ks(a,k);%this is the new Ks which will allocate in e2 had=e2-e1; had=abs(had); end – deansam Jul 16 '13 at 17:29
  • If e1 and e2 are vectors, you need to use while any(had>5), otherwise if even a single element has a difference of less than 5, your loop will finish before you want it to. – Hugh Nolan Jul 16 '13 at 19:44
1

One other way to inspect the difference vector is to use "find()" function in MATLAB. As Nolan, I think you better use the absolute value of the difference. idx = find(abs(a-b)>threshold) will give you the indices that exceed the threshold. If null, then you terminate your iterations.

ramino
  • 488
  • 4
  • 12