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

1 回表示 (過去 30 日間)
Wajeeha Nasar
Wajeeha Nasar 2016 年 10 月 7 日
コメント済み: Marc Jakobi 2016 年 10 月 8 日
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

採用された回答

Marc Jakobi
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 件のコメント
Wajeeha Nasar
Wajeeha Nasar 2016 年 10 月 8 日
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 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
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)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by