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

Celestie Gladys
Celestie Gladys 2023년 1월 26일
even though the last column dosent chage , it should be displayed ?
Fangjun Jiang
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)?
Celestie Gladys
Celestie Gladys 2023년 1월 26일
@Fangjun JiangThe data I have changes the entire column, or it dosent change at all , so I asked about the entire column .

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

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2023년 1월 26일
편집: Fangjun Jiang 2023년 1월 26일

0 개 추천

data=[3 3 4 4 5 5
6 6 5 5 8 8
9 9 7 7 2 2];
index=diff(data,1,2)
index = 3×5
0 1 0 1 0 0 -1 0 3 0 0 -2 0 -5 0
index2=any(index)
index2 = 1×5 logical array
0 1 0 1 0
output=sum(index2)
output = 2
OutData=data(:,[true,index2])
OutData = 3×3
3 4 5 6 5 8 9 7 2

댓글 수: 7

Celestie Gladys
Celestie Gladys 2023년 1월 26일
@Fangjun JiangI require also to diaplay the changes in data like this ?
3 4 5
6 5 8
9 7 2
data=[3 3 4 4 5 5
6 6 5 5 8 8
9 9 7 7 2 2];
unique(data', 'rows', 'stable')'
ans = 3×3
3 4 5 6 5 8 9 7 2
Fangjun Jiang
Fangjun Jiang 2023년 1월 26일
See update in the Answer
Celestie Gladys
Celestie Gladys 2023년 1월 26일
@Fangjun Jiang does it work for type double ?as it isnt working for me , the above is dummy data ,index and index2
Walter Roberson
Walter Roberson 2023년 1월 26일
Those 0.0000 entries you see on output are locations where the output is not exactly 0, but is smaller than 0.0001 . If you were to configure your preferences for the variable browser to use "longg" format, then you would see the actual output more clearly.
Preferences -> Variables -> Format -> Default array format: long g
Preferences -> Command Window -> MATLAB Command Window Preferences -> Text display -> Numeric format: long g
The first of those controls the variable browser. The second of those controls "disp" and display of numbers triggered by not having a semi-colon after the computation.
Celestie Gladys
Celestie Gladys 2023년 1월 26일
@Walter Roberson why does index=diff(data,1,2) dosent work and it suppose to generate [0,1]does it work on double ?
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.'
ans = 10×1
0.891461741281098 0.631347191045054 0.631347191045054 0.516064708409579 0.824509730719412 0.720280713237283 0.720280713237283 0.96542744133625 0.503147047594 0.108609798103955
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).'
ans = 9×1
-0.260114550236044 0 -0.115282482635475 0.308445022309833 -0.104229017482129 1.11022302462516e-16 0.245146728098968 -0.462280393742251 -0.394537249490044

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2023년 1월 26일

댓글:

2023년 1월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by