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

댓글을 달려면 로그인하십시오.

추가 답변 (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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by