Difference

2 visualizaciones (últimos 30 días)
Lizan
Lizan el 23 de Jul. de 2011
I have vector A =
Columns 1 through 5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Columns 6 through 10
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Columns 11 through 15
1.4602 1.4606 1.4609 1.4612 1.4615
0 0 0 0 0
0 0 0 0 0
Columns 16 through 20
1.4619 1.4622 1.4625 1.4629 1.4632
0 0 0 0 0
0 0 0 0 0
Column 21
1.4635
0
0
Why does
diff(A) =
ans =
Columns 1 through 5
0 0 0 0 0
0 0 0 0 0
Columns 6 through 10
0 0 0 0 0
0 0 0 0 0
Columns 11 through 15
-1.4602 -1.4606 -1.4609 -1.4612 -1.4615
0 0 0 0 0
Columns 16 through 20
-1.4619 -1.4622 -1.4625 -1.4629 -1.4632
0 0 0 0 0
Column 21
-1.4635
0
diff(A(1,:)) =
ans =
Columns 1 through 5
0 0 0 0 0
Columns 6 through 10
0 0 0 0 1.4602
Columns 11 through 15
0.0003 0.0003 0.0003 0.0003 0.0003
Columns 16 through 20
0.0003 0.0003 0.0003 0.0003 0.0003
give different result?
  4 comentarios
Andrei Bobrov
Andrei Bobrov el 23 de Jul. de 2011
diff(A,[],1)) % difference row-wise
Daniel Shub
Daniel Shub el 23 de Jul. de 2011
of course it did because you are not reading the documentation of diff. The [] is important. The first argument to diff is the matrix, the second is the order of the difference. You want this to be 1, which is the default so [] works or you could use 1. The third argument is the dimension over which to take the difference. Since you want the difference across rows, you want 2. Grab a pencil and paper and see which one you want.

Iniciar sesión para comentar.

Respuesta aceptada

Daniel Shub
Daniel Shub el 23 de Jul. de 2011
If I remember correctly, diff works across the first non-singleton dimension. So diff(A) works across columns and diff(A(1, :)) works across rows.
x = [1,2,3; 4,5,6];
diff(x, [], 1)
ans =
3 3 3
diff(x, [], 2)
ans =
1 1
1 1

Más respuestas (1)

Lizan
Lizan el 23 de Jul. de 2011
I want the difference per row, row one represents one solution and the second row represents another type of solution.
Which of the above will give me the correct solution for my case?

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by