I am trying to obtain a series of vectors made of elements from a larger vector given they exceed a threshold

1 回表示 (過去 30 日間)
I have Z=[6 7 8 5 6 7 8 3 2 4]' vector
for i=1:10; A(i) =Z(Z>Z(i)); end
For some reason its not working

回答 (2 件)

Matthew Eicholtz
Matthew Eicholtz 2016 年 4 月 5 日
編集済み: Matthew Eicholtz 2016 年 4 月 5 日
The reason your code does not currently work is because you are trying to assign a vector ( Z(Z>Z(i)) ) to a scalar element in a numeric array ( A(i) ). Since the size of your vector may vary on each iteration, I suggest using a cell array.
To do this, just change A(i) to A{i}.
  3 件のコメント
Matthew Eicholtz
Matthew Eicholtz 2016 年 4 月 5 日
An alternative approach if you do not want to use a cell array is to create a logical mask as follows:
Z = [6 7 8 5 6 7 8 3 2 4]';
mask = bsxfun(@gt,Z,Z');
Then, if you want to get the elements that are greater than the 4th element, for example, use:
Z(mask(:,4))

サインインしてコメントする。


Roger Stafford
Roger Stafford 2016 年 4 月 5 日
It's not clear whether you want those Z elements which exceed some fixed value or those which make up a monotone increasing sequence.
For a fixed value L:
A = Z(Z>L);
For selecting only those Z which constitute a monotone increasing sequence:
t(1) = true;
for k = 2:length(Z)
t(k) = (max(Z(1:k-1))<Z(k));
end
A = Z(t);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by