Filter löschen
Filter löschen

how to calculate element distance in a vector?

1 Ansicht (letzte 30 Tage)
sara
sara am 21 Okt. 2014
Kommentiert: sara am 21 Okt. 2014
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.

Akzeptierte Antwort

W. Owen Brimijoin
W. Owen Brimijoin am 21 Okt. 2014
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 Kommentare
W. Owen Brimijoin
W. Owen Brimijoin am 21 Okt. 2014
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 am 21 Okt. 2014
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

W. Owen Brimijoin
W. Owen Brimijoin am 21 Okt. 2014
Try this:
answer = sum((abs(diff(M,1,2))),2)
  1 Kommentar
sara
sara am 21 Okt. 2014
yes,, it works,, thank you very much,, you save my day...

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by