compare 2 consequtive columns ?
조회 수: 1 (최근 30일)
이전 댓글 표시
for example this 3x6 matrix
3 3 4 4 5 5
6 6 5 5 8 8
9 9 7 7 2 2
i want to compare two columns and display any changes in column , diplay number of times the changes occur .
the output should be
3 4 5
6 5 8
9 7 2
number times : 2
댓글 수: 3
Fangjun Jiang
2023년 1월 26일
You first and second column happen to be exactly the same. What if only one element changed, for example the second column is [3,10,9]'. Does it count as a change? What is the desired output? The number of changes? (in your case, 2)?
채택된 답변
Fangjun Jiang
2023년 1월 26일
편집: Fangjun Jiang
2023년 1월 26일
data=[3 3 4 4 5 5
6 6 5 5 8 8
9 9 7 7 2 2];
index=diff(data,1,2)
index2=any(index)
output=sum(index2)
OutData=data(:,[true,index2])
댓글 수: 7
Walter Roberson
2023년 1월 26일
diff() works fine on double, but your data might not be exactly what you think it is.
format long g
x = rand(1,10);
x(3) = x(2);
x(7) = x(6)*(1+eps);
x.'
entry 3 should be exactly the same as entry 2, so we should see an exact 0 difference there
entry 7 displays exactly the same as entry 6, but is different in the final bits, so we should not see an exact 0 difference there
diff(x,1,2).'
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!