i have a column matrix [23; 34;22;13]. i need to create set of column matrixs ,which give 1 and -1 in numbers if the next number is less than and larger than.

1 回表示 (過去 30 日間)
A=[23; 34;22;13] output matrixs=[1;-1;-1],[-1,-1],[-1]

採用された回答

Guillaume
Guillaume 2016 年 10 月 2 日
One possibility:
A = [23; 34; 22; 13];
out = arrayfun(@(idx) sign(A(idx+1:end) - A(idx)), 1:numel(A)-1, 'UniformOutput', false)
celldisp(out)
  9 件のコメント
Guillaume
Guillaume 2016 年 10 月 3 日
arrsum = cellfun(@sum, out);
totalsum = sum(arrsum)
will do

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

その他の回答 (1 件)

Atsushi Ueno
Atsushi Ueno 2016 年 10 月 2 日
編集済み: Atsushi Ueno 2016 年 10 月 3 日
I have modified the last answer after getting your comment.
A = [23; 34; 22; 13];
B = sign(diff(A));
matrixs = {0};
for i = numel(B):-1:1
matrixs = {B(i:end), matrixs{:}};
end

カテゴリ

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