# how does a diff operation works for relational and logical cases with arrays ?

2 visualizzazioni (ultimi 30 giorni)
mayank ghugretkar il 7 Giu 2019
Risposto: Star Strider il 7 Giu 2019
i was trying to work with diff operator after trying out sum[1 2 4 6]
diff([14 5 6 14 32])
ans=
-9 1 8 18
above statement is understandable.
diff([14 5 6 14 32]==14)
ans=
-1 0 1 -1
this i can't understand !
how is this working???
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

madhan ravi il 7 Giu 2019
A = [14 5 6 14 32]==14 % Split it into two lines
diff(A)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Star Strider il 7 Giu 2019
The argument creates a logical array:
Lv = [14 5 6 14 32]==14
Lv =
1×5 logical array
1 0 0 1 0
and logical arrayss become numeric when you do calculations with them:
D = diff(Lv)
D =
-1 0 1 -1
Similarly:
v = [14 5 6 14 32];
LogicalIndexing = v(Lv)
Multiplication = v .* Lv
LogicalIndexing =
14 14
Multiplication =
14 0 0 14 0
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Arithmetic Operations in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by