How can I detect the difference b/w indices of vector and stop it as well?

2 visualizaciones (últimos 30 días)
Hi, I'm doing my research on recovering spectrum. For that i'm proposing an algorithm which is much like Block Orthogonal Matching pursuit(BOMP). But the problem I'm facing is that I want my algorithm to detect the difference b/w indices of vector and stop the loop if the difference is greater than set threshold? To achieve that I'm doing this, which isn't right. A=100*100 matrix and r is 100*1 matrix.
xt=A*r;
x=find(xt>(max(abs(xt))-threshold));
for j=2:1:length(x)
xdiff(j)=x(j)-x(j-1)<Other_threshold;
break;
end
Thanks;
WN

Respuesta aceptada

Marc Jakobi
Marc Jakobi el 7 de Oct. de 2016
Editada: Marc Jakobi el 7 de Oct. de 2016
You should include an IF statement
x_diff = abs([inf; diff(x)]);
ind = find(x_diff < Other_threshold, 1); %index where difference is smaller than threshold
for i = 1:ind
% do stuff
end
  2 comentarios
Wajeeha Nasar
Wajeeha Nasar el 8 de Oct. de 2016
Thanks, it was really helpful. Now I want to create the loop that'll check difference in vector indices and whenever it is greater than other_threshold. it will stop and store the detected values in other vector. for that I doing the following code but its not working.I want to set the loop for that purpose but I don't know how to create relation between x_diff and x with setting threshold.
% code
Other_threshold=4; %setting threshold for finding difference between x
x_diff=abs([inf; diff(x)]);
x_diff(1)=0;
si=x_diff<S_threshold;
s1=x(1:si);
s2=x((si+1): end);
%concatenate the obtained vectors in S
S=[s1, s2];
I'll appreciate your help. WN
Marc Jakobi
Marc Jakobi el 8 de Oct. de 2016
I'm not quite sure I understand what you're trying to do. But I'll give it a try.
si = x_diff < S_threshold;
creates a logical vector with the same size as x_diff with true in the positions where x_diff is smaller than S_threshold and false where x_diff is greater than or equal to S_threshold. So you might want to do it like this:
si = x_diff < S_threshold;
s1 = x(si); % values smaller than S_threshold
s2 = x(~si); % values greater than S_threshold
S = [s1, s2]; % note: This only works if s1 and s2 are the same size
% i. e. there must be as many values in x_diff that are greater than or equal to S_threshold
% as there are values that are smaller than S_threshold

Iniciar sesión para comentar.

Más respuestas (1)

Massimo Zanetti
Massimo Zanetti el 7 de Oct. de 2016
Editada: Massimo Zanetti el 7 de Oct. de 2016
To get the differences between all consecutive values of x, better use the built-in matlab function diff(), and then thresholding the result:
%logical vector where ones are places where diff(x) is less than other_thr
I=diff(x)<other_thr
If you need to know the first index k at which x(k+1)-x(k)<other_thr, you can have it by
k = find(I)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by