finding index of a function

1 回表示 (過去 30 日間)
shobhit mehrotra
shobhit mehrotra 2015 年 1 月 27 日
回答済み: Thomas Feja 2015 年 1 月 27 日
Hello, I have the following code, When the cell array is created it contains values of AA when the condition is meet. I want to also create a cell array of the index of AA, AA(n) n = 1:length AA.
AA = [ 1 2 3 5 7 10 9 11 13 14 17 19 17 22 25];
data = mat2cell(AA, 1, diff([0 find(diff(AA) < 0) numel(AA)]))
Thanks!
  1 件のコメント
shobhit mehrotra
shobhit mehrotra 2015 年 1 月 27 日
Im trying to create another data cell array with indices that meet the condition. dataind = [(1, 2, 3, 4, 5, 6), (7,8,9, 10, 11, 12), (13,14,15)]

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

採用された回答

Thomas Feja
Thomas Feja 2015 年 1 月 27 日
If you want go for a single line solution, this will work:
data = arrayfun(@(x,y)x:y,[1,1+find(diff(AA)<0)],[find(diff(AA)<0),numel(AA)],'UniformOutput',false)
This is compact but hard to read. So you might prefer this solution:
idxNegDiff = [find(diff(AA)<0),numel(AA)];
start = 1;
for idx = 1:length(idxNegDiff)
c{idx} = start:idxNegDiff(idx);
start = idxNegDiff(idx)+1;
end
Either way you can verify the result using:
celldisp(c)

その他の回答 (0 件)

カテゴリ

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