How can I detect the difference b/w indices of vector and stop it as well?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Marc Jakobi
2016-10-7
编辑:Marc Jakobi
2016-10-7
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 个评论
Marc Jakobi
2016-10-8
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
更多回答(1 个)
Massimo Zanetti
2016-10-7
编辑:Massimo Zanetti
2016-10-7
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!