Finding difference of array using alternative indexes

5 visualizaciones (últimos 30 días)
shane watson
shane watson el 4 de Jun. de 2021
Comentada: Adam Danz el 9 de Jun. de 2021
Hello everyone,
I hope you're doing, I've simple question I've array (1, 24), now I want to findout the difference and divide, like [element1-element2 element2- element3 element3-element4...............element24-element23], what I did as follows
for i=1:24
a=1x24
%first case
a1(i)=a(i)-a(i+1)./i-(i+1),
% Second case
a1(i)=a(i)-a(i-1)./i-(i-1)
end
However, it is clear the index causing error (first case: Index exceeds the number of array elements (24). second case: Array indices must be positive integers or logical values.)
, could you please help me out in this situation.
  6 comentarios
shane watson
shane watson el 7 de Jun. de 2021
@Adam Danz I'm sorry for the late response, however, the issue was "reduction of index and value" so in my case I need 1 x 24 matix at the end while it is reduces to 23, and your given last two steps are good but the it creates the same problem.
Adam Danz
Adam Danz el 7 de Jun. de 2021
I'm trying to show you that the loss of one value when numerically differentiating is not a problem - it's exactly the expected behavior. Carefully look at my previous comment again to understand why you're losing a value.
I'll add an answer to suggest an alternative.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 7 de Jun. de 2021
Editada: Adam Danz el 7 de Jun. de 2021
The loss of 1 value when differentiating with diff(x,1) is the expected behavior. This function computes the difference between adjacent values in a matrix [a b c d] and there is n-1 comparisons.
Perhaps you're looking for the numeric gradient.
Comparison between gradient and diff
y = exp([1:.1:3]);
d = diff(y);
g = gradient(y);
size(y)
ans = 1×2
1 21
size(d)
ans = 1×2
1 20
size(g)
ans = 1×2
1 21
x = 1:numel(y)
x = 1×21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
hold on
plot(x(2:end),d,'b-','DisplayName','diff')
plot(x,g,'r-','DisplayName','gradient')
legend
  2 comentarios
shane watson
shane watson el 9 de Jun. de 2021
@Adam Danz, Thanks for the detail answer, I understood what you mentioned about loss of 1 value when differentiating, however as you showed the comparision of diff and gradient almost same, it is difficult to digest, espically in case when i'm computing the load demand of households and any single value changes it is problem for me. For example I used your example with sightly different values of "y" then the graph seems pretty different incase of diff and gradient.
ni=[6 7 8 9 7 5 5 6 7 8];
d = diff(ni);
g = gradient(ni);
x = 1:numel(ni);
hold on
plot(x(2:end),d,'b-','DisplayName','diff')
plot(x,g,'r-','DisplayName','gradient')
legend
Adam Danz
Adam Danz el 9 de Jun. de 2021
I need to know more about your goal. In your original question, the indexing error was caused by the loss of 1 value due to differentiating using diff(). Maybe you don't need to differentiate. Maybe you need to differentiate using a different method. Or maybe what you're doing is fine and you need to understand the output.

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 4 de Jun. de 2021
Editada: KSSV el 4 de Jun. de 2021
n = length(a) ;
iwant = (a(2:n)-a(1:n-1))./((2:n)-(1:n-1)) ;
Also have a look on geadient.
  4 comentarios
shane watson
shane watson el 4 de Jun. de 2021
Editada: shane watson el 4 de Jun. de 2021
@KSSVIt's not showing the right results unfortunately. :(

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by