Finding difference of array using alternative indexes

4 vues (au cours des 30 derniers jours)
shane watson
shane watson le 4 Juin 2021
Commenté : Adam Danz le 9 Juin 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 commentaires
shane watson
shane watson le 7 Juin 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 le 7 Juin 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.

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 7 Juin 2021
Modifié(e) : Adam Danz le 7 Juin 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 commentaires
shane watson
shane watson le 9 Juin 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 le 9 Juin 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.

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 4 Juin 2021
Modifié(e) : KSSV le 4 Juin 2021
n = length(a) ;
iwant = (a(2:n)-a(1:n-1))./((2:n)-(1:n-1)) ;
Also have a look on geadient.
  4 commentaires
KSSV
KSSV le 4 Juin 2021
iwant = gradient(a) ;
shane watson
shane watson le 4 Juin 2021
Modifié(e) : shane watson le 4 Juin 2021
@KSSVIt's not showing the right results unfortunately. :(

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by