필터 지우기
필터 지우기

how to calculate element distance in a vector?

조회 수: 2 (최근 30일)
sara
sara 2014년 10월 21일
댓글: sara 2014년 10월 21일
hi guys i want to calculate the difference between vector element as [0 1 1 0 1 1 0] which should give 0-1=1,1-1=0,1-0=1,0-1=1,1-1=0,1-0=1,,, and the sum answer should be 4. how to do this. and what if there's 12 vector within one matrix.

채택된 답변

W. Owen Brimijoin
W. Owen Brimijoin 2014년 10월 21일
It looks like you want the sum of the absolute value of the differential of those numbers? You get your first answer like this:
abs_diff = abs(diff([0 1 1 0 1 1 0]))
You would then sum these values. If all you want is the final answer, this is how to do it:
answer = sum(abs(diff([0 1 1 0 1 1 0])))
  댓글 수: 3
W. Owen Brimijoin
W. Owen Brimijoin 2014년 10월 21일
In order to answer this you would need to explain what you are looking for: one number per row, one per column, a giant sum (and if so in what dimension)?
sara
sara 2014년 10월 21일
what i'm looking for is the number per row, answer :3 2 2 for the above matrix. and i need it to calculate to the end of the rows.

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

추가 답변 (1개)

W. Owen Brimijoin
W. Owen Brimijoin 2014년 10월 21일
Try this:
answer = sum((abs(diff(M,1,2))),2)
  댓글 수: 1
sara
sara 2014년 10월 21일
yes,, it works,, thank you very much,, you save my day...

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by