Subtracting vector elements, with an if condition?
조회 수: 13 (최근 30일)
이전 댓글 표시
Hey guys,
I need to subtract every element in my array with the element next to it and before it, then if the result of the subtraction is 0 on either subtraction, I need a new array where the elements which weren't 0 are keep their original values, but the elements which were 0 stay 0. Is this possible?
a=[1 2 3 4 5 6 7 7 7 7 7 8 9 10 11 12 13];
b=[1 2 3 4 5 6 0 0 0 0 0 8 9 10 11 12 13];
Thanks in advance!
댓글 수: 0
채택된 답변
추가 답변 (2개)
Walter Roberson
2018년 5월 10일
b = a;
mask = b(1:end-1) = b(2:end);
b([false,mask]|[mask|false]) = 0;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!